Permalink
Browse files

Merge remote-tracking branch 'mshibuya/mongoid' into mongoid

Conflicts:
	app/controllers/rails_admin/main_controller.rb
	lib/rails_admin/adapters/active_record.rb
	lib/rails_admin/config/fields/factories/belongs_to_association.rb
  • Loading branch information...
2 parents e450630 + 3536cdc commit 0b6ec58f1e09fa80fa62124bac5ff5c3fec3552d @bbenezech bbenezech committed Mar 5, 2012
Showing with 1,533 additions and 62 deletions.
  1. +2 −0 Gemfile
  2. +6 −1 Rakefile
  3. +9 −7 app/controllers/rails_admin/main_controller.rb
  4. +4 −0 config/initializers/mongoid_extensions.rb
  5. +24 −1 lib/rails_admin/abstract_model.rb
  6. +15 −16 lib/rails_admin/adapters/active_record.rb
  7. +333 −0 lib/rails_admin/adapters/mongoid.rb
  8. +32 −0 lib/rails_admin/adapters/mongoid/abstract_object.rb
  9. +27 −0 lib/rails_admin/adapters/mongoid/extension.rb
  10. +4 −4 lib/rails_admin/config/fields/base.rb
  11. +1 −1 lib/rails_admin/config/fields/factories/belongs_to_association.rb
  12. +2 −1 lib/rails_admin/config/fields/types.rb
  13. +4 −0 lib/rails_admin/config/fields/types/all.rb
  14. +23 −0 lib/rails_admin/config/fields/types/array.rb
  15. +1 −1 lib/rails_admin/config/fields/types/belongs_to_association.rb
  16. +35 −0 lib/rails_admin/config/fields/types/bson_object_id.rb
  17. +2 −2 lib/rails_admin/config/fields/types/enum.rb
  18. +23 −0 lib/rails_admin/config/fields/types/hash.rb
  19. +25 −0 lib/rails_admin/config/fields/types/mongoid_type.rb
  20. +1 −1 lib/rails_admin/config/fields/types/polymorphic_association.rb
  21. +2 −0 rails_admin.gemspec
  22. +2 −0 spec/dummy_app/Gemfile
  23. +8 −0 spec/dummy_app/app/models/article.rb
  24. +6 −0 spec/dummy_app/app/models/author.rb
  25. +22 −0 spec/dummy_app/app/models/mongoid_field_test.rb
  26. +17 −0 spec/dummy_app/config/mongoid.yml
  27. +14 −0 spec/factories.rb
  28. +28 −0 spec/integration/basic/update/rails_admin_basic_update_spec.rb
  29. +10 −0 spec/spec_helper.rb
  30. +336 −27 spec/unit/adapters/active_record_spec.rb
  31. +30 −0 spec/unit/adapters/mongoid/abstract_object_spec.rb
  32. +485 −0 spec/unit/adapters/mongoid_spec.rb
View
@@ -29,6 +29,8 @@ group :development, :test do
end
end
+ gem 'bson_ext'
+ gem 'mongoid'
gem 'cancan'
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
@@ -80,6 +80,14 @@ def properties
end
end
+ def table_name
+ model.table_name
+ end
+
+ def serialized_attributes
+ model.serialized_attributes
+ end
+
private
def query_conditions(query, fields = config.list.fields.select(&:queryable?))
@@ -158,6 +166,8 @@ def build_statement(column, type, value, operator)
"%#{value}"
when 'is', '='
"#{value}"
+ else
+ return
end
["(#{column} #{LIKE_OPERATOR} ?)", value]
when :datetime, :timestamp, :date
@@ -173,13 +183,15 @@ def build_statement(column, type, value, operator)
[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]
+ return ["(#{column} >= ?)", value.to_i.days.ago]
when 'more_than'
return if value.blank?
- [2000.years.ago, value.to_i.days.ago]
+ return ["(#{column} <= ?)", 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]
+ else
+ return
end
["(#{column} BETWEEN ? AND ?)", *values]
when :enum
@@ -188,22 +200,9 @@ 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
- 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
Oops, something went wrong.

0 comments on commit 0b6ec58

Please sign in to comment.