Skip to content
Browse files

Merge remote-tracking branch 'upstream/master' into mongoid

Conflicts:
	lib/rails_admin/adapters/active_record.rb
  • Loading branch information...
2 parents 566270a + d0aea5e commit 8c214c914f349126d49f3150833e4decde278e2c @mshibuya committed Mar 10, 2012
View
4 app/controllers/rails_admin/main_controller.rb
@@ -133,8 +133,8 @@ def get_association_scope_from_params
source_model_config = source_abstract_model.config
source_object = source_abstract_model.get(params[:source_object_id])
action = params[:current_action].in?(['create', 'update']) ? params[:current_action] : 'edit'
- association = source_model_config.send(action).fields.find{|f| f.name == params[:associated_collection].to_sym }.with(:controller => self, :object => source_object)
- association.associated_collection_scope
+ @association = source_model_config.send(action).fields.find{|f| f.name == params[:associated_collection].to_sym }.with(:controller => self, :object => source_object)
+ @association.associated_collection_scope
end
def associations_hash
View
4 app/views/rails_admin/main/_form_filtering_multiselect.html.haml
@@ -18,10 +18,10 @@
xhr = !field.associated_collection_cache_all
collection = if xhr
- selected.map { |o| [o.send(field.associated_label_method), o.send(field.associated_primary_key)] }
+ selected.map { |o| [o.send(field.associated_object_label_method), o.send(field.associated_primary_key)] }
else
i = 0
- controller.list_entries(config, :index, field.associated_collection_scope, false).map { |o| [o.send(field.associated_label_method), o.send(field.associated_primary_key)] }.sort_by {|a| [selected_ids.index(a[1]) || selected_ids.size, i+=1] }
+ controller.list_entries(config, :index, field.associated_collection_scope, false).map { |o| [o.send(field.associated_object_label_method), o.send(field.associated_primary_key)] }.sort_by {|a| [selected_ids.index(a[1]) || selected_ids.size, i+=1] }
end
%input{:name => form.dom_name(field), :type => "hidden", :value => ""}
View
4 app/views/rails_admin/main/_form_filtering_select.html.haml
@@ -6,7 +6,7 @@
if form.object.new_record? && related_id.present? && related_id != 'new'
selected = config.abstract_model.get(related_id)
selected_id = selected.send(field.associated_primary_key)
- selected_name = selected.send(field.associated_label_method)
+ selected_name = selected.send(field.associated_object_label_method)
else
selected_id = field.selected_id
selected_name = field.formatted_value
@@ -18,7 +18,7 @@
xhr = !field.associated_collection_cache_all
- collection = xhr ? [[selected_name, selected_id]] : controller.list_entries(config, :index, field.associated_collection_scope, false).map { |o| [o.send(field.associated_label_method), o.send(field.associated_primary_key)] }
+ collection = xhr ? [[selected_name, selected_id]] : controller.list_entries(config, :index, field.associated_collection_scope, false).map { |o| [o.send(field.associated_object_label_method), o.send(field.associated_primary_key)] }
- selected_id = (hdv = field.html_default_value).nil? ? selected_id : hdv
= form.select field.method_name, collection, { :selected => selected_id, :include_blank => true }, field.html_attributes.reverse_merge({:placeholder => t('admin.misc.search')})
View
34 lib/rails_admin/adapters/active_record.rb
@@ -5,7 +5,13 @@ module RailsAdmin
module Adapters
module ActiveRecord
DISABLED_COLUMN_TYPES = [:tsvector, :blob, :binary, :spatial]
- LIKE_OPERATOR = ::ActiveRecord::Base.configurations[Rails.env]['adapter'] == "postgresql" ? 'ILIKE' : 'LIKE'
+ 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))
@@ -46,6 +52,10 @@ def count(options = {}, scope = nil)
def destroy(objects)
Array.wrap(objects).each &:destroy
end
+
+ def primary_key
+ model.primary_key
+ end
def associations
model.reflect_on_all_associations.map do |association|
@@ -171,7 +181,27 @@ def build_statement(column, type, value, operator)
end
["(#{column} #{LIKE_OPERATOR} ?)", value]
when :datetime, :timestamp, :date
- start_date, end_date = 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? ? (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
if start_date && end_date
["(#{column} BETWEEN ? AND ?)", start_date, end_date]
View
4 lib/rails_admin/config/actions/index.rb
@@ -37,7 +37,9 @@ class Index < RailsAdmin::Config::Actions::Base
format.json do
output = if params[:compact]
- @objects.map{ |o| { :id => o.id, :label => o.send(@model_config.object_label_method) } }
+ primary_key_method = @association ? @association.associated_primary_key : @model_config.abstract_model.primary_key
+ label_method = @model_config.object_label_method
+ @objects.map{ |o| { :id => o.send(primary_key_method), :label => o.send(label_method) } }
else
@objects.to_json(@schema)
end
View
4 lib/rails_admin/config/fields/association.rb
@@ -60,8 +60,8 @@ def associated_model_config
end
# Reader for the association's child model object's label method
- def associated_label_method
- @associated_label_method ||= associated_model_config.object_label_method
+ def associated_object_label_method
+ @associated_object_label_method ||= associated_model_config.object_label_method
end
# Reader for associated primary key
View
13 spec/controllers/main_controller_spec.rb
@@ -110,6 +110,19 @@
end
end
+
+ describe "index" do
+ it "uses source association's primary key with :compact, not target model's default primary key" do
+ class TeamWithNumberedPlayers < ActiveRecord::Base
+ self.table_name = 'teams'
+ has_many :numbered_players, :class_name => 'Player', :primary_key => :number, :foreign_key => 'team_id'
+ end
+ FactoryGirl.create :team
+ TeamWithNumberedPlayers.first.numbered_players = [FactoryGirl.create(:player, :number => 123)]
+ returned = get :index, {:model_name => 'player', :source_object_id => Team.first.id, :source_abstract_model => 'team_with_numbered_players', :associated_collection => 'numbered_players', :current_action => :create, :compact => true, :format => :json}
+ returned.body.should =~ /\"id\"\:123/
+ end
+ end
describe "sanitize_params_for!" do
it 'sanitize params recursively in nested forms' do

0 comments on commit 8c214c9

Please sign in to comment.
Something went wrong with that request. Please try again.