Module: RailsAdmin::Adapters::ActiveRecord
- Defined in:
- lib/rails_admin/adapters/active_record.rb
Constant Summary collapse
- DISABLED_COLUMN_TYPES =
[:tsvector]
- @@polymorphic_parents =
nil
Class Method Summary collapse
Instance Method Summary collapse
- #all(options = {}, scope = nil) ⇒ Object
- #association_as_lookup(association) ⇒ Object
- #association_child_key_lookup(association) ⇒ Object
- #association_child_model_lookup(association) ⇒ Object
- #association_foreign_type_lookup(association) ⇒ Object
- #association_inverse_of_lookup(association) ⇒ Object
- #association_options(association) ⇒ Object
- #association_parent_key_lookup(association) ⇒ Object
- #association_parent_model_lookup(association) ⇒ Object
- #association_polymorphic_lookup(association) ⇒ Object
- #association_read_only_lookup(association) ⇒ Object
- #associations ⇒ Object
- #belongs_to_associations ⇒ Object
- #build_statement(column, type, value, operator) ⇒ Object
- #count(options = {}, scope = nil) ⇒ Object
- #create(params = {}) ⇒ Object
- #destroy(objects) ⇒ Object
- #destroy_all! ⇒ Object
- #first(options = {}, scope = nil) ⇒ Object
- #get(id) ⇒ Object
- #get_conditions_hash(model_config, query, filters) ⇒ Object
- #has_and_belongs_to_many_associations ⇒ Object
- #has_many_associations ⇒ Object
- #has_one_associations ⇒ Object
- #model_store_exists? ⇒ Boolean
- #new(params = {}) ⇒ Object
- #polymorphic_associations ⇒ Object
- #properties ⇒ Object
- #scoped ⇒ Object
Class Method Details
.extended(abstract_model) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/rails_admin/adapters/active_record.rb', line 10 def self.extended(abstract_model) # ActiveRecord does not handle has_one relationships the way it does for has_many, # and does not create any association_id and association_id= methods. # Added here for backward compatibility after a refactoring, but it does belong to ActiveRecord IMO. # Support is hackish at best. Atomicity is respected for creation, but not while updating. # It means a failed validation at update on the parent object could still modify target belongs_to foreign ids. # # abstract_model.model.reflect_on_all_associations.select{|assoc| assoc.macro.to_s == 'has_one'}.each do |association| abstract_model.model.send(:define_method, "#{association.name}_id") do self.send(association.name).try(:id) end abstract_model.model.send(:define_method, "#{association.name}_id=") do |id| self.send(association.name.to_s + '=', associated = (id.blank? ? nil : association.klass.find_by_id(id))) end end end |
.polymorphic_parents(name) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rails_admin/adapters/active_record.rb', line 29 def self.polymorphic_parents(name) @@polymorphic_parents ||= {}.tap do |hash| RailsAdmin::AbstractModel.all_models.each do |klass| klass.reflect_on_all_associations.select{|r| r.[:as] }.each do |reflection| (hash[reflection.[:as].to_sym] ||= []) << klass end end end @@polymorphic_parents[name.to_sym] end |
Instance Method Details
#all(options = {}, scope = nil) ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/rails_admin/adapters/active_record.rb', line 56 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([:conditions]) if [:conditions] scope = scope.page([:page]).per([:per]) if [:page] && [:per] scope = scope.reorder("#{[:sort]} #{[:sort_reverse] ? 'asc' : 'desc'}") if [:sort] scope end |
#association_as_lookup(association) ⇒ Object
316 317 318 |
# File 'lib/rails_admin/adapters/active_record.rb', line 316 def association_as_lookup(association) association.[:as].try :to_sym end |
#association_child_key_lookup(association) ⇒ Object
347 348 349 350 351 352 353 354 355 356 |
# File 'lib/rails_admin/adapters/active_record.rb', line 347 def association_child_key_lookup(association) case association.macro when :belongs_to association.[:foreign_key].try(:to_sym) || "#{association.name}_id".to_sym when :has_one, :has_many, :has_and_belongs_to_many association.foreign_key.to_sym else raise "Unknown association type: #{association.macro.inspect}" end end |
#association_child_model_lookup(association) ⇒ Object
336 337 338 339 340 341 342 343 344 345 |
# File 'lib/rails_admin/adapters/active_record.rb', line 336 def association_child_model_lookup(association) case association.macro when :belongs_to association.active_record when :has_one, :has_many, :has_and_belongs_to_many association.klass else raise "Unknown association type: #{association.macro.inspect}" end end |
#association_foreign_type_lookup(association) ⇒ Object
310 311 312 313 314 |
# File 'lib/rails_admin/adapters/active_record.rb', line 310 def association_foreign_type_lookup(association) if association.[:polymorphic] association.[:foreign_type].try(:to_sym) || :"#{association.name}_type" end end |
#association_inverse_of_lookup(association) ⇒ Object
328 329 330 |
# File 'lib/rails_admin/adapters/active_record.rb', line 328 def association_inverse_of_lookup(association) association.[:inverse_of].try :to_sym end |
#association_options(association) ⇒ Object
280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/rails_admin/adapters/active_record.rb', line 280 def (association) if association.[:polymorphic] { :polymorphic => true, :foreign_type => association.[:foreign_type] || "#{association.name}_type" } elsif association.[:as] { :as => association.[:as] } else {} end end |
#association_parent_key_lookup(association) ⇒ Object
324 325 326 |
# File 'lib/rails_admin/adapters/active_record.rb', line 324 def association_parent_key_lookup(association) [:id] end |
#association_parent_model_lookup(association) ⇒ Object
295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/rails_admin/adapters/active_record.rb', line 295 def association_parent_model_lookup(association) case association.macro when :belongs_to if association.[:polymorphic] RailsAdmin::Adapters::ActiveRecord.polymorphic_parents(association.name) || [] else association.klass end when :has_one, :has_many, :has_and_belongs_to_many association.active_record else raise "Unknown association type: #{association.macro.inspect}" end end |
#association_polymorphic_lookup(association) ⇒ Object
320 321 322 |
# File 'lib/rails_admin/adapters/active_record.rb', line 320 def association_polymorphic_lookup(association) association.[:polymorphic] end |
#association_read_only_lookup(association) ⇒ Object
332 333 334 |
# File 'lib/rails_admin/adapters/active_record.rb', line 332 def association_read_only_lookup(association) association.[:readonly] end |
#associations ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/rails_admin/adapters/active_record.rb', line 113 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, :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), :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) } end end |
#belongs_to_associations ⇒ Object
107 108 109 110 111 |
# File 'lib/rails_admin/adapters/active_record.rb', line 107 def belongs_to_associations associations.select do |association| association[:type] == :belongs_to end end |
#build_statement(column, type, value, operator) ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/rails_admin/adapters/active_record.rb', line 214 def build_statement(column, type, value, operator) # this operator/value has been discarded (but kept in the dom to override the one stored in the various links of the page) return if operator == '_discard' || value == '_discard' # filtering data with unary operator, not type dependent if operator == '_blank' || value == '_blank' return ["(#{column} IS NULL OR #{column} = '')"] elsif operator == '_present' || value == '_present' return ["(#{column} IS NOT NULL AND #{column} != '')"] elsif operator == '_null' || value == '_null' return ["(#{column} IS NULL)"] elsif operator == '_not_null' || value == '_not_null' return ["(#{column} IS NOT NULL)"] elsif operator == '_empty' || value == '_empty' return ["(#{column} = '')"] elsif operator == '_not_empty' || value == '_not_empty' return ["(#{column} != '')"] end # now we go type specific case type when :boolean return ["(#{column} IS NULL OR #{column} = ?)", false] if ['false', 'f', '0'].include?(value) return ["(#{column} = ?)", true] if ['true', 't', '1'].include?(value) when :integer, :belongs_to_association return if value.blank? ["(#{column} = ?)", value.to_i] if value.to_i.to_s == value when :string, :text return if value.blank? value = case operator when 'default', 'like' "%#{value}%" when 'starts_with' "#{value}%" when 'ends_with' "%#{value}" when 'is', '=' "#{value}" end ["(#{column} #{@like_operator} ?)", value] when :datetime, :timestamp, :date return unless operator != 'default' values = case operator when 'today' [Date.today.beginning_of_day, Date.today.end_of_day] when 'yesterday' [Date.yesterday.beginning_of_day, Date.yesterday.end_of_day] when 'this_week' [Date.today.beginning_of_week.beginning_of_day, Date.today.end_of_week.end_of_day] when 'last_week' [1.week.ago.to_date.beginning_of_week.beginning_of_day, 1.week.ago.to_date.end_of_week.end_of_day] when 'less_than' return if value.blank? [value.to_i.days.ago, DateTime.now] when 'more_than' return if value.blank? [2000.years.ago, value.to_i.days.ago] end ["(#{column} BETWEEN ? AND ?)", *values] when :enum return if value.blank? ["(#{column} IN (?))", [value].flatten] 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 |
#create(params = {}) ⇒ Object
71 72 73 |
# File 'lib/rails_admin/adapters/active_record.rb', line 71 def create(params = {}) model.create(params) end |
#destroy(objects) ⇒ Object
79 80 81 |
# File 'lib/rails_admin/adapters/active_record.rb', line 79 def destroy(objects) [objects].flatten.map &:destroy end |
#destroy_all! ⇒ Object
83 84 85 86 87 |
# File 'lib/rails_admin/adapters/active_record.rb', line 83 def destroy_all! model.all.each do |object| object.destroy end end |
#first(options = {}, scope = nil) ⇒ Object
52 53 54 |
# File 'lib/rails_admin/adapters/active_record.rb', line 52 def first( = {}, scope = nil) all(, scope).first end |
#get(id) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/rails_admin/adapters/active_record.rb', line 40 def get(id) if object = model.where(model.primary_key => id).first RailsAdmin::AbstractObject.new object else nil end end |
#get_conditions_hash(model_config, query, filters) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/rails_admin/adapters/active_record.rb', line 156 def get_conditions_hash(model_config, query, filters) @like_operator = "ILIKE" if ::ActiveRecord::Base.configurations[Rails.env]['adapter'] == "postgresql" @like_operator ||= "LIKE" query_statements = [] filters_statements = [] values = [] conditions = [""] if query.present? queryable_fields = model_config.list.fields.select(&:queryable?) queryable_fields.each do |field| searchable_columns = field.searchable_columns.flatten searchable_columns.each do |field_infos| statement, value1, value2 = build_statement(field_infos[:column], field_infos[:type], query, field.search_operator) if statement query_statements << statement values << value1 unless value1.nil? values << value2 unless value2.nil? end end end end unless query_statements.empty? conditions[0] += " AND " unless conditions == [""] conditions[0] += "(#{query_statements.join(" OR ")})" # any column field will do end if filters.present? @filterable_fields = model_config.list.fields.select(&:filterable?).inject({}){ |memo, field| memo[field.name.to_sym] = field.searchable_columns; memo } filters.each_pair do |field_name, filters_dump| filters_dump.each do |filter_index, filter_dump| field_statements = [] @filterable_fields[field_name.to_sym].each do |field_infos| unless filter_dump[:disabled] statement, value1, value2 = build_statement(field_infos[:column], field_infos[:type], filter_dump[:value], (filter_dump[:operator] || 'default')) if statement field_statements << statement values << value1 unless value1.nil? values << value2 unless value2.nil? end end end filters_statements << "(#{field_statements.join(' OR ')})" unless field_statements.empty? end end end unless filters_statements.empty? conditions[0] += " AND " unless conditions == [""] conditions[0] += "#{filters_statements.join(" AND ")}" # filters should all be true end conditions += values conditions != [""] ? { :conditions => conditions } : {} end |
#has_and_belongs_to_many_associations ⇒ Object
89 90 91 92 93 |
# File 'lib/rails_admin/adapters/active_record.rb', line 89 def has_and_belongs_to_many_associations associations.select do |association| association[:type] == :has_and_belongs_to_many end end |
#has_many_associations ⇒ Object
95 96 97 98 99 |
# File 'lib/rails_admin/adapters/active_record.rb', line 95 def has_many_associations associations.select do |association| association[:type] == :has_many end end |
#has_one_associations ⇒ Object
101 102 103 104 105 |
# File 'lib/rails_admin/adapters/active_record.rb', line 101 def has_one_associations associations.select do |association| association[:type] == :has_one end end |
#model_store_exists? ⇒ Boolean
152 153 154 |
# File 'lib/rails_admin/adapters/active_record.rb', line 152 def model_store_exists? model.table_exists? end |
#new(params = {}) ⇒ Object
75 76 77 |
# File 'lib/rails_admin/adapters/active_record.rb', line 75 def new(params = {}) RailsAdmin::AbstractObject.new(model.new(params)) end |
#polymorphic_associations ⇒ Object
132 133 134 135 136 |
# File 'lib/rails_admin/adapters/active_record.rb', line 132 def polymorphic_associations (has_many_associations + has_one_associations).select do |association| association[:options][:as] end end |
#properties ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/rails_admin/adapters/active_record.rb', line 138 def properties columns = model.columns.reject {|c| 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
67 68 69 |
# File 'lib/rails_admin/adapters/active_record.rb', line 67 def scoped model.scoped end |