Module: MerbAdmin::AbstractModel::DatamapperSupport

Defined in:
lib/datamapper_support.rb

Defined Under Namespace

Modules: InstanceMethods

Instance Method Summary collapse

Instance Method Details

#all(options = {}) ⇒ Object



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

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

#associationsObject



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/datamapper_support.rb', line 71

def associations
  model.relationships.to_a.map do |name, association|
    {
      :name => name,
      :pretty_name => name.to_s.gsub("_", " ").capitalize,
      :type => association_type_lookup(association),
      :parent_model => association.parent_model,
      :parent_key => association.parent_key.map{|r| r.name},
      :child_model => association.child_model,
      :child_key => association.child_key.map{|r| r.name},
    }
  end
end

#belongs_to_associationsObject



65
66
67
68
69
# File 'lib/datamapper_support.rb', line 65

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

#count(options = {}) ⇒ Object



12
13
14
15
# File 'lib/datamapper_support.rb', line 12

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

#create(params = {}) ⇒ Object



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

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

#destroy_all!Object



49
50
51
# File 'lib/datamapper_support.rb', line 49

def destroy_all!
  model.all.destroy!
end

#first(options = {}) ⇒ Object



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

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

#get(id) ⇒ Object



8
9
10
# File 'lib/datamapper_support.rb', line 8

def get(id)
  model.get(id).extend(InstanceMethods)
end

#has_many_associationsObject



53
54
55
56
57
# File 'lib/datamapper_support.rb', line 53

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

#has_one_associationsObject



59
60
61
62
63
# File 'lib/datamapper_support.rb', line 59

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

#new(params = {}) ⇒ Object



45
46
47
# File 'lib/datamapper_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/datamapper_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



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/datamapper_support.rb', line 85

def properties
  model.properties.map do |property|
    {
      :name => property.name,
      :pretty_name => property.name.to_s.gsub(/_id$/, "").gsub("_", " ").capitalize,
      :type => type_lookup(property),
      :length => property.length,
      :nullable? => property.nullable?,
      :serial? => property.serial?,
    }
  end
end