Module: MerbAdmin::AbstractModel::ActiverecordSupport
- Defined in:
- lib/activerecord_support.rb
Defined Under Namespace
Modules: InstanceMethods
Instance Method Summary collapse
- #all(options = {}) ⇒ Object
- #all_in(ids, 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
18 19 20 |
# File 'lib/activerecord_support.rb', line 18 def all( = {}) model.all() end |
#all_in(ids, options = {}) ⇒ Object
22 23 24 25 |
# File 'lib/activerecord_support.rb', line 22 def all_in(ids, = {}) [:conditions] = ["id IN (?)", ids] model.all() end |
#associations ⇒ Object
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_associations ⇒ Object
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( = {}) model.count() 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( = {}) model.first().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_associations ⇒ Object
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_associations ⇒ Object
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( = {}) 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
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 |