Mongoid support #1031

Merged
merged 42 commits into from Mar 12, 2012
Commits
Jump to file
+1,665 −104
Split
View
@@ -29,6 +29,8 @@ group :development, :test do
end
end
+ gem 'bson_ext'
+ gem 'mongoid'
gem 'cancan'
end
@@ -40,6 +42,7 @@ group :debug do
platform :mri_19 do
gem 'ruby-debug19'
+ gem 'simplecov', :require => false
end
end
View
@@ -11,4 +11,9 @@ RSpec::Core::RakeTask.new(:spec)
task :test => :spec
task :default => :spec
-
+namespace :spec do
+ task :coverage do
+ ENV['INVOKE_SIMPLECOV'] = 'true'
+ Rake::Task[:spec].invoke
+ end
+end
@@ -30,8 +30,10 @@ def bulk_action
end
def list_entries(model_config = @model_config, auth_scope_key = :index, additional_scope = get_association_scope_from_params, pagination = !(params[:associated_collection] || params[:all]))
- scope = @authorization_adapter && @authorization_adapter.query(auth_scope_key, model_config.abstract_model)
- scope = model_config.abstract_model.scoped.merge(scope)
+ scope = model_config.abstract_model.scoped
+ if auth_scope = @authorization_adapter && @authorization_adapter.query(auth_scope_key, model_config.abstract_model)
+ scope = scope.merge(auth_scope)
+ end
scope = scope.instance_eval(&additional_scope) if additional_scope
get_collection(model_config, scope, pagination)
@@ -53,23 +55,23 @@ def get_sort_hash(model_config)
field = model_config.list.fields.find{ |f| f.name.to_s == params[:sort] }
column = if field.nil? || field.sortable == true # use params[:sort] on the base table
- "#{abstract_model.model.table_name}.#{params[:sort]}"
+ "#{abstract_model.table_name}.#{params[:sort]}"
elsif field.sortable == false # use default sort, asked field is not sortable
- "#{abstract_model.model.table_name}.#{model_config.list.sort_by}"
+ "#{abstract_model.table_name}.#{model_config.list.sort_by}"
elsif field.sortable.is_a?(String) && field.sortable.include?('.') # just provide sortable, don't do anything smart
field.sortable
elsif field.sortable.is_a?(Hash) # just join sortable hash, don't do anything smart
"#{field.sortable.keys.first}.#{field.sortable.values.first}"
elsif field.association? # use column on target table
- "#{field.associated_model_config.abstract_model.model.table_name}.#{field.sortable}"
+ "#{field.associated_model_config.abstract_model.table_name}.#{field.sortable}"
else # use described column in the field conf.
- "#{abstract_model.model.table_name}.#{field.sortable}"
+ "#{abstract_model.table_name}.#{field.sortable}"
end
reversed_sort = (field ? field.sort_reverse? : model_config.list.sort_reverse?)
{:sort => column, :sort_reverse => (params[:sort_reverse] == reversed_sort.to_s)}
end
-
+
def redirect_to_on_success
notice = t("admin.flash.successful", :name => @model_config.label, :action => t("admin.actions.#{@action.key}.done"))
if params[:_add_another]
@@ -0,0 +1,4 @@
+if defined?(::Mongoid::Document)
+ require 'rails_admin/adapters/mongoid/extension'
+ Mongoid::Document.send(:include, RailsAdmin::Adapters::Mongoid::Extension)
+end
@@ -20,15 +20,38 @@ def new(m)
rescue LoadError, NameError
nil
end
+
+ @@polymorphic_parents = {}
+
+ def polymorphic_parents(adapter, name)
+ @@polymorphic_parents[adapter.to_sym] ||= {}.tap do |hash|
+ all(adapter).each do |am|
+ am.associations.select{|r| r[:as] }.each do |association|
+ (hash[association[:as].to_sym] ||= []) << am.model
+ end
+ end
+ end
+ @@polymorphic_parents[adapter.to_sym][name.to_sym]
+ end
+
+ # For testing
+ def reset_polymorphic_parents
+ @@polymorphic_parents = {}
+ end
end
def initialize(m)
@model_name = m.to_s
- # ActiveRecord
if m.ancestors.map(&:to_s).include?('ActiveRecord::Base') && !m.abstract_class?
+ # ActiveRecord
@adapter = :active_record
require 'rails_admin/adapters/active_record'
extend Adapters::ActiveRecord
+ elsif m.ancestors.map(&:to_s).include?('Mongoid::Document')
+ # Mongoid
+ @adapter = :mongoid
+ require 'rails_admin/adapters/mongoid'
+ extend Adapters::Mongoid
end
end
@@ -52,5 +75,36 @@ def param_key
def pretty_name
model.model_name.human
end
+
+ private
+
+ def get_filtering_duration(operator, value)
+ date_format = I18n.t("admin.misc.filter_date_format", :default => I18n.t("admin.misc.filter_date_format", :locale => :en)).gsub('dd', '%d').gsub('mm', '%m').gsub('yy', '%Y')
+ case operator
+ when 'between'
+ start_date = value[1].present? ? (beginning_of_date(Date.strptime(value[1], date_format)) rescue false) : false
+ end_date = value[2].present? ? (Date.strptime(value[2], date_format).end_of_day rescue false) : false
+ when 'today'
+ start_date = beginning_of_date(Date.today)
+ end_date = Date.today.end_of_day
+ when 'yesterday'
+ start_date = beginning_of_date(Date.yesterday)
+ end_date = Date.yesterday.end_of_day
+ when 'this_week'
+ start_date = beginning_of_date(Date.today.beginning_of_week)
+ end_date = Date.today.end_of_week.end_of_day
+ when 'last_week'
+ start_date = beginning_of_date(1.week.ago.to_date.beginning_of_week)
+ end_date = 1.week.ago.to_date.end_of_week.end_of_day
+ else # default
+ start_date = (beginning_of_date(Date.strptime(Array.wrap(value).first, date_format)) rescue false)
+ end_date = (Date.strptime(Array.wrap(value).first, date_format).end_of_day rescue false)
+ end
+ [start_date, end_date]
+ end
+
+ def beginning_of_date(date)
+ date.beginning_of_day
+ end
end
end
@@ -7,11 +7,6 @@ module ActiveRecord
DISABLED_COLUMN_TYPES = [:tsvector, :blob, :binary, :spatial]
AR_ADAPTER = ::ActiveRecord::Base.configurations[Rails.env]['adapter']
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
def new(params = {})
AbstractObject.new(model.new(params))
@@ -90,6 +85,14 @@ def properties
end
end
+ def table_name
+ model.table_name
+ end
+
+ def serialized_attributes
+ model.serialized_attributes.keys
+ end
+
private
def query_conditions(query, fields = config.list.fields.select(&:queryable?))
@@ -168,30 +171,12 @@ def build_statement(column, type, value, operator)
"%#{value}"
when 'is', '='
"#{value}"
+ else
+ return
end
["(#{column} #{LIKE_OPERATOR} ?)", value]
when :datetime, :timestamp, :date
- date_format = I18n.t("admin.misc.filter_date_format", :default => I18n.t("admin.misc.filter_date_format", :locale => :en)).gsub('dd', '%d').gsub('mm', '%m').gsub('yy', '%Y')
- case operator
- when 'between'
- start_date = value[1].present? ? (Date.strptime(value[1], date_format).instance_eval(&BEGINNING_OF_DAY) rescue false) : false
- end_date = value[2].present? ? (Date.strptime(value[2], date_format).end_of_day rescue false) : false
- when 'today'
- start_date = Date.today.instance_eval(&BEGINNING_OF_DAY)
- end_date = Date.today.end_of_day
- when 'yesterday'
- start_date = Date.yesterday.instance_eval(&BEGINNING_OF_DAY)
- end_date = Date.yesterday.end_of_day
- when 'this_week'
- start_date = Date.today.beginning_of_week.instance_eval(&BEGINNING_OF_DAY)
- end_date = Date.today.end_of_week.end_of_day
- when 'last_week'
- start_date = 1.week.ago.to_date.beginning_of_week.instance_eval(&BEGINNING_OF_DAY)
- end_date = 1.week.ago.to_date.end_of_week.end_of_day
- else # default
- start_date = (Date.strptime(Array.wrap(value).first, date_format).instance_eval(&BEGINNING_OF_DAY) rescue false)
- end_date = (Date.strptime(Array.wrap(value).first, date_format).end_of_day rescue false)
- end
+ start_date, end_date = get_filtering_duration(operator, value)
if start_date && end_date
["(#{column} BETWEEN ? AND ?)", start_date, end_date]
@@ -206,22 +191,19 @@ def build_statement(column, type, value, operator)
end
end
- @@polymorphic_parents = nil
-
- def self.polymorphic_parents(name)
- @@polymorphic_parents ||= {}.tap do |hash|
- RailsAdmin::AbstractModel.all(:active_record).each do |am|
- am.model.reflect_on_all_associations.select{|r| r.options[:as] }.each do |reflection|
- (hash[reflection.options[:as].to_sym] ||= []) << am.model
- end
- end
+ if AR_ADAPTER == "postgresql"
+ def beginning_of_date(date)
+ date.beginning_of_day
+ end
+ else
+ def beginning_of_date(date)
+ date.yesterday.end_of_day
end
- @@polymorphic_parents[name.to_sym]
end
def association_model_lookup(association)
if association.options[:polymorphic]
- RailsAdmin::Adapters::ActiveRecord.polymorphic_parents(association.name) || []
+ RailsAdmin::AbstractModel.polymorphic_parents(:active_record, association.name) || []
else
association.klass
end
@@ -242,7 +224,7 @@ def association_as_lookup(association)
end
def association_polymorphic_lookup(association)
- association.options[:polymorphic]
+ !!association.options[:polymorphic]
end
def association_primary_key_lookup(association)
Oops, something went wrong.