Module: MerbAdmin::AbstractModel::DatamapperSupport

Defined in:
lib/datamapper_support.rb

Defined Under Namespace

Modules: InstanceMethods

Instance Method Summary collapse

Instance Method Details

#associationsObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/datamapper_support.rb', line 54

def associations
  model.relationships.to_a.map do |name, relationship|
    {
      :name => name,
      :pretty_name => name.to_s.gsub('_', ' '),
      :type => association_type_lookup(relationship),
      :parent_model => relationship.parent_model,
      :parent_key => relationship.parent_key.map{|r| r.name},
      :child_model => relationship.child_model,
      :child_key => relationship.child_key.map{|r| r.name},
      :remote_relationship => relationship.options[:remote_relationship_name],
      :near_relationship => relationship.options[:near_relationship_name],
    }
  end
end

#belongs_to_associationsObject



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

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

#count(options = {}) ⇒ Object



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

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

#find(id) ⇒ Object



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

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

#find_all(options = {}) ⇒ Object



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

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

#has_many_associationsObject



36
37
38
39
40
# File 'lib/datamapper_support.rb', line 36

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

#has_one_associationsObject



42
43
44
45
46
# File 'lib/datamapper_support.rb', line 42

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

#new(params = {}) ⇒ Object



32
33
34
# File 'lib/datamapper_support.rb', line 32

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

#paginated(options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/datamapper_support.rb', line 16

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

  options[:order] ||= [:id.desc]

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

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

  [page_count, find_all(options)]
end

#propertiesObject



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

def properties
  model.properties.map do |property|
    {
      :name => property.name,
      :pretty_name => property.field.gsub('_', ' '),
      :type => type_lookup(property),
      :length => property.length,
      :nullable? => property.nullable?,
      :serial? => property.serial?,
      :key? => property.key?,
      :flag_map => property.type.respond_to?(:flag_map) ? property.type.flag_map : nil,
    }
  end
end