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'
- BEGINNING_OF_DAY =
if AR_ADAPTER == "postgresql" lambda { |date| date.beginning_of_day } else lambda { |date| date.yesterday.end_of_day } end
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
Instance Method Details
#all(options = {}, scope = nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rails_admin/adapters/active_record.rb', line 36 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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rails_admin/adapters/active_record.rb', line 60 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
48 49 50 |
# File 'lib/rails_admin/adapters/active_record.rb', line 48 def count( = {}, scope = nil) all(.merge({:limit => false, :page => false}), scope).count end |
#destroy(objects) ⇒ Object
52 53 54 |
# File 'lib/rails_admin/adapters/active_record.rb', line 52 def destroy(objects) Array.wrap(objects).each &:destroy end |
#first(options = {}, scope = nil) ⇒ Object
32 33 34 |
# File 'lib/rails_admin/adapters/active_record.rb', line 32 def first( = {}, scope = nil) all(, scope).first end |
#get(id) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/rails_admin/adapters/active_record.rb', line 20 def get(id) if object = model.where(model.primary_key => id).first AbstractObject.new object else nil end end |
#new(params = {}) ⇒ Object
16 17 18 |
# File 'lib/rails_admin/adapters/active_record.rb', line 16 def new(params = {}) AbstractObject.new(model.new(params)) end |
#primary_key ⇒ Object
56 57 58 |
# File 'lib/rails_admin/adapters/active_record.rb', line 56 def primary_key model.primary_key end |
#properties ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/rails_admin/adapters/active_record.rb', line 79 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
28 29 30 |
# File 'lib/rails_admin/adapters/active_record.rb', line 28 def scoped model.scoped end |