Module: MerbAdmin::AbstractModel::ActiverecordSupport
- Defined in:
- lib/active_record_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
- #last(options = {}) ⇒ Object
- #new(params = {}) ⇒ Object
- #paginated(options = {}) ⇒ Object
- #properties ⇒ Object
Instance Method Details
#all(options = {}) ⇒ Object
24 25 26 |
# File 'lib/active_record_support.rb', line 24 def all( = {}) model.all(merge_order()) end |
#associations ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/active_record_support.rb', line 74 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
68 69 70 71 72 |
# File 'lib/active_record_support.rb', line 68 def belongs_to_associations associations.select do |association| association[:type] == :belongs_to end end |
#count(options = {}) ⇒ Object
12 13 14 |
# File 'lib/active_record_support.rb', line 12 def count( = {}) model.count(.reject{|key, value| [:sort, :sort_reverse].include?(key)}) end |
#create(params = {}) ⇒ Object
42 43 44 |
# File 'lib/active_record_support.rb', line 42 def create(params = {}) model.create(params).extend(InstanceMethods) end |
#destroy_all! ⇒ Object
50 51 52 53 54 |
# File 'lib/active_record_support.rb', line 50 def destroy_all! model.all.each do |object| object.destroy end end |
#first(options = {}) ⇒ Object
16 17 18 |
# File 'lib/active_record_support.rb', line 16 def first( = {}) model.first(merge_order()).extend(InstanceMethods) end |
#get(id) ⇒ Object
6 7 8 9 10 |
# File 'lib/active_record_support.rb', line 6 def get(id) model.find_by_id(id).extend(InstanceMethods) rescue ActiveRecord::RecordNotFound nil end |
#has_many_associations ⇒ Object
56 57 58 59 60 |
# File 'lib/active_record_support.rb', line 56 def has_many_associations associations.select do |association| association[:type] == :has_many end end |
#has_one_associations ⇒ Object
62 63 64 65 66 |
# File 'lib/active_record_support.rb', line 62 def has_one_associations associations.select do |association| association[:type] == :has_one end end |
#last(options = {}) ⇒ Object
20 21 22 |
# File 'lib/active_record_support.rb', line 20 def last( = {}) model.last(merge_order()).extend(InstanceMethods) end |
#new(params = {}) ⇒ Object
46 47 48 |
# File 'lib/active_record_support.rb', line 46 def new(params = {}) model.new(params).extend(InstanceMethods) end |
#paginated(options = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/active_record_support.rb', line 28 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
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/active_record_support.rb', line 88 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, } end end |