Module: MerbAdmin::AbstractModel::ActiverecordSupport

Defined in:
lib/activerecord_support.rb

Defined Under Namespace

Modules: InstanceMethods

Instance Method Summary collapse

Instance Method Details

#all(options = {}) ⇒ Object



18
19
20
# File 'lib/activerecord_support.rb', line 18

def all(options = {})
  model.all(options)
end

#all_in(ids, options = {}) ⇒ Object



22
23
24
25
# File 'lib/activerecord_support.rb', line 22

def all_in(ids, options = {})
  options[:conditions] = ["id IN (?)", ids]
  model.all(options)
end

#associationsObject



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/activerecord_support.rb', line 73

def associations
  model.reflect_on_all_associations.map do |association|
    {
      :name => association.name,
      :pretty_name => association.name.to_s.gsub('_', ' ').capitalize,
      :type => association.macro,
      :parent_model => association_parent_model_lookup(association),
      :parent_key => association_parent_key_lookup(association),
      :child_model => association_child_model_lookup(association),
      :child_key => association_child_key_lookup(association),
    }
  end
end

#belongs_to_associationsObject



67
68
69
70
71
# File 'lib/activerecord_support.rb', line 67

def belongs_to_associations
  associations.select do |association|
    association[:type] == :belongs_to
  end
end

#count(options = {}) ⇒ Object



4
5
6
# File 'lib/activerecord_support.rb', line 4

def count(options = {})
  model.count(options)
end

#create(params = {}) ⇒ Object



41
42
43
# File 'lib/activerecord_support.rb', line 41

def create(params = {})
  model.create(params)
end

#destroy_all!Object



49
50
51
52
53
# File 'lib/activerecord_support.rb', line 49

def destroy_all!
  model.all.each do |object|
    object.destroy
  end
end

#first(options = {}) ⇒ Object



14
15
16
# File 'lib/activerecord_support.rb', line 14

def first(options = {})
  model.first(options).extend(InstanceMethods)
end

#get(id) ⇒ Object



8
9
10
11
12
# File 'lib/activerecord_support.rb', line 8

def get(id)
  model.find(id).extend(InstanceMethods)
rescue
  nil
end

#has_many_associationsObject



55
56
57
58
59
# File 'lib/activerecord_support.rb', line 55

def has_many_associations
  associations.select do |association|
    association[:type] == :has_many
  end
end

#has_one_associationsObject



61
62
63
64
65
# File 'lib/activerecord_support.rb', line 61

def has_one_associations
  associations.select do |association|
    association[:type] == :has_one
  end
end

#new(params = {}) ⇒ Object



45
46
47
# File 'lib/activerecord_support.rb', line 45

def new(params = {})
  model.new(params).extend(InstanceMethods)
end

#paginated(options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/activerecord_support.rb', line 27

def paginated(options = {})
  page = options.delete(:page) || 1
  per_page = options.delete(:per_page) || MerbAdmin[:per_page]

  page_count = (count(options).to_f / per_page).ceil

  options.merge!({
    :limit => per_page,
    :offset => (page - 1) * per_page
  })

  [page_count, all(options)]
end

#propertiesObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/activerecord_support.rb', line 87

def properties
  model.columns.map do |property|
    {
      :name => property.name.to_sym,
      :pretty_name => property.human_name,
      :type => property.type,
      :length => property.limit,
      :nullable? => property.null,
      :serial? => property.primary,
      :key? => property.primary,
      :flag_map => property.type.respond_to?(:flag_map) ? property.type.flag_map : nil,
    }
  end
end