Module: MerbAdmin::AbstractModel::DatamapperSupport
- Defined in:
- lib/datamapper_support.rb
Defined Under Namespace
Modules: InstanceMethods
Instance Method Summary collapse
- #all(options = {}) ⇒ Object
- #associations ⇒ Object
- #belongs_to_associations ⇒ Object
- #count(options = {}) ⇒ Object
- #create(params = {}) ⇒ Object
- #destroy_all! ⇒ Object
- #first(options = {}) ⇒ Object
- #get(id) ⇒ Object
- #has_many_associations ⇒ Object
- #has_one_associations ⇒ Object
- #new(params = {}) ⇒ Object
- #paginated(options = {}) ⇒ Object
- #properties ⇒ Object
Instance Method Details
#all(options = {}) ⇒ Object
21 22 23 |
# File 'lib/datamapper_support.rb', line 21 def all( = {}) model.all(merge_order()) end |
#associations ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/datamapper_support.rb', line 69 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_associations ⇒ Object
63 64 65 66 67 |
# File 'lib/datamapper_support.rb', line 63 def belongs_to_associations associations.select do |association| association[:type] == :belongs_to end end |
#count(options = {}) ⇒ Object
13 14 15 |
# File 'lib/datamapper_support.rb', line 13 def count( = {}) model.count(.reject{|key, value| [:sort, :sort_reverse].include?(key)}) end |
#create(params = {}) ⇒ Object
39 40 41 |
# File 'lib/datamapper_support.rb', line 39 def create(params = {}) model.create(params).extend(InstanceMethods) end |
#destroy_all! ⇒ Object
47 48 49 |
# File 'lib/datamapper_support.rb', line 47 def destroy_all! model.all.destroy! end |
#first(options = {}) ⇒ Object
17 18 19 |
# File 'lib/datamapper_support.rb', line 17 def first( = {}) model.first(merge_order()).extend(InstanceMethods) end |
#get(id) ⇒ Object
9 10 11 |
# File 'lib/datamapper_support.rb', line 9 def get(id) model.get(id).extend(InstanceMethods) end |
#has_many_associations ⇒ Object
51 52 53 54 55 |
# File 'lib/datamapper_support.rb', line 51 def has_many_associations associations.select do |association| association[:type] == :has_many end end |
#has_one_associations ⇒ Object
57 58 59 60 61 |
# File 'lib/datamapper_support.rb', line 57 def has_one_associations associations.select do |association| association[:type] == :has_one end end |
#new(params = {}) ⇒ Object
43 44 45 |
# File 'lib/datamapper_support.rb', line 43 def new(params = {}) model.new(params).extend(InstanceMethods) end |
#paginated(options = {}) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/datamapper_support.rb', line 25 def paginated( = {}) page = .delete(:page) || 1 per_page = .delete(:per_page) || MerbAdmin[:per_page] page_count = (count().to_f / per_page).ceil .merge!({ :limit => per_page, :offset => (page - 1) * per_page }) [page_count, all()] end |
#properties ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/datamapper_support.rb', line 83 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.respond_to?(:length) ? property.length : nil, :nullable? => property.allow_nil?, :serial? => property.serial?, } end end |