Skip to content

Commit

Permalink
Merge remote-tracking branch 'mshibuya/mongoid' into mongoid
Browse files Browse the repository at this point in the history
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
bbenezech committed Mar 5, 2012
2 parents e450630 + 3536cdc commit 0b6ec58
Show file tree
Hide file tree
Showing 32 changed files with 1,533 additions and 62 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -29,6 +29,8 @@ group :development, :test do
end
end

gem 'bson_ext'
gem 'mongoid'
gem 'cancan'
end

Expand Down
7 changes: 6 additions & 1 deletion Rakefile
Expand Up @@ -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
16 changes: 9 additions & 7 deletions app/controllers/rails_admin/main_controller.rb
Expand Up @@ -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)
Expand All @@ -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]
Expand Down
4 changes: 4 additions & 0 deletions config/initializers/mongoid_extensions.rb
@@ -0,0 +1,4 @@
if defined?(::Mongoid::Document)
require 'rails_admin/adapters/mongoid/extension'
Mongoid::Document.send(:include, RailsAdmin::Adapters::Mongoid::Extension)
end
25 changes: 24 additions & 1 deletion lib/rails_admin/abstract_model.rb
Expand Up @@ -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

Expand Down
31 changes: 15 additions & 16 deletions lib/rails_admin/adapters/active_record.rb
Expand Up @@ -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?))
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 0b6ec58

Please sign in to comment.