Module: RailsAdmin::Adapters::ActiveRecord
- Defined in:
- lib/rails_admin/adapters/active_record.rb
Constant Summary collapse
- DISABLED_COLUMN_TYPES =
[:tsvector, :blob, :binary]
- @@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_nested_attributes_options_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
.polymorphic_parents(name) ⇒ Object
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/rails_admin/adapters/active_record.rb', line 11 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
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rails_admin/adapters/active_record.rb', line 38 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
304 305 306 |
# File 'lib/rails_admin/adapters/active_record.rb', line 304 def association_as_lookup(association) association.[:as].try :to_sym end |
#association_child_key_lookup(association) ⇒ Object
335 336 337 338 339 340 341 342 343 344 |
# File 'lib/rails_admin/adapters/active_record.rb', line 335 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
324 325 326 327 328 329 330 331 332 333 |
# File 'lib/rails_admin/adapters/active_record.rb', line 324 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
294 295 296 297 298 |
# File 'lib/rails_admin/adapters/active_record.rb', line 294 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
316 317 318 |
# File 'lib/rails_admin/adapters/active_record.rb', line 316 def association_inverse_of_lookup(association) association.[:inverse_of].try :to_sym end |
#association_nested_attributes_options_lookup(association) ⇒ Object
300 301 302 |
# File 'lib/rails_admin/adapters/active_record.rb', line 300 def (association) model..try { |o| o[association.name.to_sym] } end |
#association_options(association) ⇒ Object
264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/rails_admin/adapters/active_record.rb', line 264 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
312 313 314 |
# File 'lib/rails_admin/adapters/active_record.rb', line 312 def association_parent_key_lookup(association) [:id] end |
#association_parent_model_lookup(association) ⇒ Object
279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/rails_admin/adapters/active_record.rb', line 279 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
308 309 310 |
# File 'lib/rails_admin/adapters/active_record.rb', line 308 def association_polymorphic_lookup(association) association.[:polymorphic] end |
#association_read_only_lookup(association) ⇒ Object
320 321 322 |
# File 'lib/rails_admin/adapters/active_record.rb', line 320 def association_read_only_lookup(association) association.[:readonly] end |
#associations ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/rails_admin/adapters/active_record.rb', line 95 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), :nested_form => (association) } end end |
#belongs_to_associations ⇒ Object
89 90 91 92 93 |
# File 'lib/rails_admin/adapters/active_record.rb', line 89 def belongs_to_associations associations.select do |association| association[:type] == :belongs_to end end |
#build_statement(column, type, value, operator) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 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 |
# File 'lib/rails_admin/adapters/active_record.rb', line 195 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] when 'mmddyyyy' return if (value.blank? || value.match(/([0-9]{8})/).nil?) [Date.strptime(value.match(/([0-9]{8})/)[1], '%m%d%Y').beginning_of_day, Date.strptime(value.match(/([0-9]{8})/)[1], '%m%d%Y').end_of_day] end ["(#{column} BETWEEN ? AND ?)", *values] when :enum return if value.blank? ["(#{column} IN (?))", [value].flatten] end end |
#count(options = {}, scope = nil) ⇒ Object
30 31 32 |
# File 'lib/rails_admin/adapters/active_record.rb', line 30 def count( = {}, scope = nil) all(.merge({:limit => false, :page => false}), scope).count end |
#create(params = {}) ⇒ Object
53 54 55 |
# File 'lib/rails_admin/adapters/active_record.rb', line 53 def create(params = {}) model.create(params) end |
#destroy(objects) ⇒ Object
61 62 63 |
# File 'lib/rails_admin/adapters/active_record.rb', line 61 def destroy(objects) [objects].flatten.map &:destroy end |
#destroy_all! ⇒ Object
65 66 67 68 69 |
# File 'lib/rails_admin/adapters/active_record.rb', line 65 def destroy_all! model.all.each do |object| object.destroy end end |
#first(options = {}, scope = nil) ⇒ Object
34 35 36 |
# File 'lib/rails_admin/adapters/active_record.rb', line 34 def first( = {}, scope = nil) all(, scope).first end |
#get(id) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/rails_admin/adapters/active_record.rb', line 22 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
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 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 |
# File 'lib/rails_admin/adapters/active_record.rb', line 139 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 ")})" 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| statement, value1, value2 = build_statement(field_infos[:column], field_infos[:type], filter_dump[:v], (filter_dump[:o] || 'default')) if statement field_statements << statement values << value1 unless value1.nil? values << value2 unless value2.nil? 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
71 72 73 74 75 |
# File 'lib/rails_admin/adapters/active_record.rb', line 71 def has_and_belongs_to_many_associations associations.select do |association| association[:type] == :has_and_belongs_to_many end end |
#has_many_associations ⇒ Object
77 78 79 80 81 |
# File 'lib/rails_admin/adapters/active_record.rb', line 77 def has_many_associations associations.select do |association| association[:type] == :has_many end end |
#has_one_associations ⇒ Object
83 84 85 86 87 |
# File 'lib/rails_admin/adapters/active_record.rb', line 83 def has_one_associations associations.select do |association| association[:type] == :has_one end end |
#model_store_exists? ⇒ Boolean
135 136 137 |
# File 'lib/rails_admin/adapters/active_record.rb', line 135 def model_store_exists? model.table_exists? end |
#new(params = {}) ⇒ Object
57 58 59 |
# File 'lib/rails_admin/adapters/active_record.rb', line 57 def new(params = {}) RailsAdmin::AbstractObject.new(model.new(params)) end |
#polymorphic_associations ⇒ Object
115 116 117 118 119 |
# File 'lib/rails_admin/adapters/active_record.rb', line 115 def polymorphic_associations (has_many_associations + has_one_associations).select do |association| association[:options][:as] end end |
#properties ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/rails_admin/adapters/active_record.rb', line 121 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
49 50 51 |
# File 'lib/rails_admin/adapters/active_record.rb', line 49 def scoped model.scoped end |