Module: RailsAdmin::Adapters::ActiveRecord
- Defined in:
- lib/rails_admin/adapters/active_record.rb,
lib/rails_admin/adapters/active_record/abstract_object.rb
Defined Under Namespace
Classes: AbstractObject
Constant Summary collapse
- DISABLED_COLUMN_TYPES =
[:tsvector, :blob, :binary, :spatial]
- AR_ADAPTER =
::ActiveRecord::Base.configurations[Rails.env]
- LIKE_OPERATOR =
AR_ADAPTER == "postgresql" ? 'ILIKE' : 'LIKE'
Instance Method Summary collapse
- #all(options = {}, scope = nil) ⇒ Object
- #associations ⇒ Object
- #count(options = {}, scope = nil) ⇒ Object
- #destroy(objects) ⇒ Object
- #first(options = {}, scope = nil) ⇒ Object
- #get(id) ⇒ Object
- #new(params = {}) ⇒ Object
- #primary_key ⇒ Object
- #properties ⇒ Object
- #scoped ⇒ Object
- #serialized_attributes ⇒ Object
- #table_name ⇒ Object
Instance Method Details
#all(options = {}, scope = nil) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rails_admin/adapters/active_record.rb', line 31 def all( = {}, scope = nil) scope ||= self.scoped scope = scope.includes([:include]) if [:include] scope = scope.limit([:limit]) if [:limit] scope = scope.where(model.primary_key => [:bulk_ids]) if [:bulk_ids] scope = scope.where(query_conditions([:query])) if [:query] scope = scope.where(filter_conditions([:filters])) if [:filters] scope = scope.page([:page]).per([:per]) if [:page] && [:per] scope = scope.reorder("#{[:sort]} #{[:sort_reverse] ? 'asc' : 'desc'}") if [:sort] scope end |
#associations ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/rails_admin/adapters/active_record.rb', line 55 def associations model.reflect_on_all_associations.map do |association| { :name => association.name.to_sym, :pretty_name => association.name.to_s.tr('_', ' ').capitalize, :type => association.macro, :model_proc => Proc.new { association_model_lookup(association) }, :primary_key_proc => Proc.new { association_primary_key_lookup(association) }, :foreign_key => association_foreign_key_lookup(association), :foreign_type => association_foreign_type_lookup(association), :as => association_as_lookup(association), :polymorphic => association_polymorphic_lookup(association), :inverse_of => association_inverse_of_lookup(association), :read_only => association_read_only_lookup(association), :nested_form => (association) } end end |
#count(options = {}, scope = nil) ⇒ Object
43 44 45 |
# File 'lib/rails_admin/adapters/active_record.rb', line 43 def count( = {}, scope = nil) all(.merge({:limit => false, :page => false}), scope).count end |
#destroy(objects) ⇒ Object
47 48 49 |
# File 'lib/rails_admin/adapters/active_record.rb', line 47 def destroy(objects) Array.wrap(objects).each &:destroy end |
#first(options = {}, scope = nil) ⇒ Object
27 28 29 |
# File 'lib/rails_admin/adapters/active_record.rb', line 27 def first( = {}, scope = nil) all(, scope).first end |
#get(id) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/rails_admin/adapters/active_record.rb', line 15 def get(id) if object = model.where(model.primary_key => id).first AbstractObject.new object else nil end end |
#new(params = {}) ⇒ Object
11 12 13 |
# File 'lib/rails_admin/adapters/active_record.rb', line 11 def new(params = {}) AbstractObject.new(model.new(params)) end |
#primary_key ⇒ Object
51 52 53 |
# File 'lib/rails_admin/adapters/active_record.rb', line 51 def primary_key model.primary_key end |
#properties ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rails_admin/adapters/active_record.rb', line 74 def properties columns = model.columns.reject {|c| c.type.blank? || DISABLED_COLUMN_TYPES.include?(c.type.to_sym) } columns.map do |property| { :name => property.name.to_sym, :pretty_name => property.name.to_s.tr('_', ' ').capitalize, :type => property.type, :length => property.limit, :nullable? => property.null, :serial? => property.primary, } end end |
#scoped ⇒ Object
23 24 25 |
# File 'lib/rails_admin/adapters/active_record.rb', line 23 def scoped model.scoped end |
#serialized_attributes ⇒ Object
92 93 94 |
# File 'lib/rails_admin/adapters/active_record.rb', line 92 def serialized_attributes model.serialized_attributes.keys end |
#table_name ⇒ Object
88 89 90 |
# File 'lib/rails_admin/adapters/active_record.rb', line 88 def table_name model.table_name end |