Skip to content
Browse files

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

Conflicts:
	lib/rails_admin/adapters/active_record.rb
  • Loading branch information...
2 parents 662cb54 + 1486f43 commit abbd06c37953794bed730989fa2623ce65f4632c @bbenezech bbenezech committed
View
57 app/assets/javascripts/rails_admin/ra.filter-box.js
@@ -3,7 +3,15 @@
var filters;
$.filters = filters = {
- append: function(field_label, field_name, field_type, field_value, field_operator, field_options, multiple_values, index) {
+ options: {
+ regional: {
+ datePicker: {
+ dateFormat: 'mm/dd/yy'
+ }
+ }
+ },
+
+ append: function(field_label, field_name, field_type, field_value, field_operator, field_options, index) {
var value_name = 'f[' + field_name + '][' + index + '][v]';
var operator_name = 'f[' + field_name + '][' + index + '][o]';
switch(field_type) {
@@ -21,22 +29,23 @@
case 'datetime':
case 'timestamp':
var control = '<select class="switch-additionnal-fieldsets input-small" name="' + operator_name + '">' +
- '<option data-additional-fieldset="false" value="_discard">...</option>' +
- '<option data-additional-fieldset="false"' + (field_operator == "today" ? 'selected="selected"' : '') + ' value="today">Today</option>' +
- '<option data-additional-fieldset="false"' + (field_operator == "yesterday" ? 'selected="selected"' : '') + ' value="yesterday">Yesterday</option>' +
- '<option data-additional-fieldset="false"' + (field_operator == "this_week" ? 'selected="selected"' : '') + ' value="this_week">This week</option>' +
- '<option data-additional-fieldset="false"' + (field_operator == "last_week" ? 'selected="selected"' : '') + ' value="last_week">Last week</option>' +
- '<option data-additional-fieldset="true" ' + (field_operator == "less_than" ? 'selected="selected"' : '') + ' value="less_than">Less than ... days ago</option>' +
- '<option data-additional-fieldset="true" ' + (field_operator == "more_than" ? 'selected="selected"' : '') + ' value="more_than">More than ... days ago</option>' +
- '<option data-additional-fieldset="true" ' + (field_operator == "mmddyyyy" ? 'selected="selected"' : '') + ' value="mmddyyyy">On specific date (mmddyyyy)</option>' +
+ '<option ' + (field_operator == "default" ? 'selected="selected"' : '') + ' data-additional-fieldset="default" value="default">Date ...</option>' +
+ '<option ' + (field_operator == "between" ? 'selected="selected"' : '') + ' data-additional-fieldset="between" value="between">Between ... and ...</option>' +
+ '<option ' + (field_operator == "today" ? 'selected="selected"' : '') + ' value="today">Today</option>' +
+ '<option ' + (field_operator == "yesterday" ? 'selected="selected"' : '') + ' value="yesterday">Yesterday</option>' +
+ '<option ' + (field_operator == "this_week" ? 'selected="selected"' : '') + ' value="this_week">This week</option>' +
+ '<option ' + (field_operator == "last_week" ? 'selected="selected"' : '') + ' value="last_week">Last week</option>' +
'<option disabled="disabled">---------</option>' +
- '<option data-additional-fieldset="false"' + (field_operator == "_not_null" ? 'selected="selected"' : '') + ' value="_not_null">Is present</option>' +
- '<option data-additional-fieldset="false"' + (field_operator == "_null" ? 'selected="selected"' : '') + ' value="_null" >Is blank</option>' +
+ '<option ' + (field_operator == "_not_null" ? 'selected="selected"' : '') + ' value="_not_null">Is present</option>' +
+ '<option ' + (field_operator == "_null" ? 'selected="selected"' : '') + ' value="_null" >Is blank</option>' +
'</select>'
- var additional_control = '<input class="additional-fieldset input-small" style="display:' + (field_operator == "less_than" || field_operator == "more_than" || field_operator == "mmddyyyy" ? 'inline-block' : 'none') + ';" type="text" name="' + value_name + '" value="' + field_value + '" /> ';
+ var additional_control =
+ '<input class="date additional-fieldset default input-small" style="display:' + ((!field_operator || field_operator == "default") ? 'inline-block' : 'none') + ';" type="text" name="' + value_name + '[]" value="' + (field_value[0] || '') + '" /> ' +
+ '<input placeholder="-∞" class="date additional-fieldset between input-small" style="display:' + ((field_operator == "between") ? 'inline-block' : 'none') + ';" type="text" name="' + value_name + '[]" value="' + (field_value[1] || '') + '" /> ' +
+ '<input placeholder="∞" class="date additional-fieldset between input-small" style="display:' + ((field_operator == "between") ? 'inline-block' : 'none') + ';" type="text" name="' + value_name + '[]" value="' + (field_value[2] || '') + '" />';
break;
case 'enum':
- var field_options = $('<div/>').html(field_options).text(); // entities decode
+ var multiple_values = ((field_value instanceof Array) ? true : false)
var control = '<select style="display:' + (multiple_values ? 'none' : 'inline-block') + '" ' + (multiple_values ? '' : 'name="' + value_name + '"') + ' data-name="' + value_name + '" class="select-single input-small">' +
'<option value="_discard">...</option>' +
'<option ' + (field_value == "_present" ? 'selected="selected"' : '') + ' value="_present">Is present</option>' +
@@ -53,13 +62,13 @@
case 'text':
case 'belongs_to_association':
var control = '<select class="switch-additionnal-fieldsets input-small" value="' + field_operator + '" name="' + operator_name + '">' +
- '<option data-additional-fieldset="true"' + (field_operator == "like" ? 'selected="selected"' : '') + ' value="like">Contains</option>' +
- '<option data-additional-fieldset="true"' + (field_operator == "is" ? 'selected="selected"' : '') + ' value="is">Is exactly</option>' +
- '<option data-additional-fieldset="true"' + (field_operator == "starts_with" ? 'selected="selected"' : '') + ' value="starts_with">Starts with</option>' +
- '<option data-additional-fieldset="true"' + (field_operator == "ends_with" ? 'selected="selected"' : '') + ' value="ends_with">Ends with</option>' +
+ '<option data-additional-fieldset="additional-fieldset"' + (field_operator == "like" ? 'selected="selected"' : '') + ' value="like">Contains</option>' +
+ '<option data-additional-fieldset="additional-fieldset"' + (field_operator == "is" ? 'selected="selected"' : '') + ' value="is">Is exactly</option>' +
+ '<option data-additional-fieldset="additional-fieldset"' + (field_operator == "starts_with" ? 'selected="selected"' : '') + ' value="starts_with">Starts with</option>' +
+ '<option data-additional-fieldset="additional-fieldset"' + (field_operator == "ends_with" ? 'selected="selected"' : '') + ' value="ends_with">Ends with</option>' +
'<option disabled="disabled">---------</option>' +
- '<option data-additional-fieldset="false"' + (field_operator == "_present" ? 'selected="selected"' : '') + ' value="_present">Is present</option>' +
- '<option data-additional-fieldset="false"' + (field_operator == "_blank" ? 'selected="selected"' : '') + ' value="_blank">Is blank</option>' +
+ '<option ' + (field_operator == "_present" ? 'selected="selected"' : '') + ' value="_present">Is present</option>' +
+ '<option ' + (field_operator == "_blank" ? 'selected="selected"' : '') + ' value="_blank">Is blank</option>' +
'</select>'
var additional_control = '<input class="additional-fieldset input-small" style="display:' + (field_operator == "_blank" || field_operator == "_present" ? 'none' : 'inline-block') + ';" type="text" name="' + value_name + '" value="' + field_value + '" /> ';
break;
@@ -74,6 +83,7 @@
(additional_control || '') +
'</div> ';
$('#filters_box').append(content);
+ $('#filters_box .date').datepicker(this.options.regional.datePicker);
$("hr.filters_box:hidden").show('slow');
}
}
@@ -87,8 +97,7 @@
$(this).data('field-value'),
$(this).data('field-operator'),
$(this).data('field-options'),
- $(this).data('field-multiple_values'),
- $.now().toString().slice(7,11)
+ $.now().toString().slice(6,11)
);
});
@@ -97,7 +106,6 @@
form = $(this).parents('form');
$(this).parents('.filter').remove();
!$("#filters_box").children().length && $("hr.filters_box:visible").hide('slow');
- form.submit();
});
$('#filters_box .switch-select').live('click', function(e) {
@@ -111,8 +119,9 @@
$('#filters_box .switch-additionnal-fieldsets').live('change', function() {
var selected_option = $(this).find('option:selected');
- if($(selected_option).data('additional-fieldset')) {
- $(this).siblings('.additional-fieldset').show('slow');
+ if(klass = $(selected_option).data('additional-fieldset')) {
+ $(this).siblings('.additional-fieldset:not(.' + klass + ')').hide('slow');
+ $(this).siblings('.' + klass).show('slow');
} else {
$(this).siblings('.additional-fieldset').hide('slow');
}
View
2 app/assets/javascripts/rails_admin/ui.js.coffee
@@ -45,7 +45,7 @@ $('.form-horizontal legend').live 'click', ->
if $(this).has('i.icon-chevron-right').length
$(this).siblings('.control-group:hidden').show('slow')
$(this).children('i').toggleClass('icon-chevron-down icon-chevron-right')
-
+
$(document).ready ->
$('.animate-width-to').each ->
View
5 app/assets/stylesheets/rails_admin/base/theming.css.scss
@@ -31,6 +31,11 @@ body.rails_admin {
/* new/edit/export forms */
.form-horizontal {
+ /* hide hidden fields controls by default */
+ .control-group .hidden_type {
+ display:none;
+ }
+
legend {
cursor:pointer;
i {
View
14 app/helpers/rails_admin/form_builder.rb
@@ -32,15 +32,11 @@ def fieldset_for fieldset, nested_in
end
def field_wrapper_for field, nested_in
- if field.is_a?(RailsAdmin::Config::Fields::Types::Hidden)
- input_for(field)
- else
- # do not show nested field if the target is the origin
- unless field.inverse_of.presence && field.inverse_of == nested_in
- @template.content_tag(:div, :class => "control-group #{field.type_css_class} #{field.css_class} #{'error' if field.errors.present?}", :id => "#{dom_id(field)}_field") do
- label(field.method_name, field.label, :class => 'control-label') +
- (field.nested_form ? field_for(field) : input_for(field))
- end
+ # do not show nested field if the target is the origin
+ unless field.inverse_of.presence && field.inverse_of == nested_in
+ @template.content_tag(:div, :class => "control-group #{field.type_css_class} #{field.css_class} #{'error' if field.errors.present?}", :id => "#{dom_id(field)}_field") do
+ label(field.method_name, field.label, :class => 'control-label') +
+ (field.nested_form ? field_for(field) : input_for(field))
end
end
end
View
26 app/views/rails_admin/main/index.html.haml
@@ -1,4 +1,5 @@
:ruby
+ require 'rails_admin/config/fields/types/datetime.rb'
query = params[:query]
params = request.params.except(:authenticity_token, :action, :controller, :utf8, :bulk_export, :_pjax)
params.delete(:query) if params[:query].blank?
@@ -38,12 +39,14 @@
field = @filterable_fields.find{ |field| field.name == filter_name.to_sym }
field_options = case field.type
when :enum
- options_for_select(field.with(:object => @abstract_model.model.new).enum, filter_hash['v']).gsub("\n", '')
+ options_for_select(field.with(:object => @abstract_model.model.new).enum, filter_hash['v'])
else
''
end
- "$.filters.append('#{escape_javascript field.label.to_s}', '#{escape_javascript field.name.to_s}', '#{escape_javascript field.type.to_s}', '#{escape_javascript filter_hash['v'].to_s}', '#{escape_javascript filter_hash['o'].to_s}', '#{escape_javascript field_options.to_s}', #{filter_hash['v'].is_a?(Array)}, '#{escape_javascript filter_index.to_s}');"
- end.join if @ordered_filters
+ %{
+ $.filters.append(#{field.label.to_json}, #{field.name.to_json}, #{field.type.to_json}, #{filter_hash['v'].to_json}, #{filter_hash['o'].to_json}, #{field_options.to_json}, #{filter_index.to_json});
+ }
+ end.join.html_safe if @ordered_filters
= content_for :contextual_tabs do
@@ -57,16 +60,29 @@
- @filterable_fields.each do |field|
- field_options = case field.type
- when :enum
- - h options_for_select(field.with(:object => @abstract_model.model.new).enum).gsub("\n", '')
+ - options_for_select(field.with(:object => @abstract_model.model.new).enum)
- else
- ''
%li
- %a{:href => '#', "data-field-label" => field.label, "data-field-name" => field.name, "data-field-options" => field_options, "data-field-type" => field.type, "data-field-value" => ""}= field.label
+ %a{:href => '#', :"data-field-label" => field.label, :"data-field-name" => field.name, :"data-field-options" => field_options.html_safe, :"data-field-type" => field.type, :"data-field-value" => ""}= field.label
#list{:'data-pjax-container' => true}
%script
jQuery(function($) {
+ $.filters.options.regional = {
+ datePicker: {
+ dateFormat: #{raw I18n.t("admin.misc.filter_date_format", :default => I18n.t("admin.misc.filter_date_format", :locale => :en)).to_json},
+ dayNames: #{raw RailsAdmin::Config::Fields::Types::Datetime.day_names.to_json},
+ dayNamesShort: #{raw RailsAdmin::Config::Fields::Types::Datetime.abbr_day_names.to_json},
+ dayNamesMin: #{raw RailsAdmin::Config::Fields::Types::Datetime.abbr_day_names.to_json},
+ firstDay: "1",
+ monthNames: #{raw RailsAdmin::Config::Fields::Types::Datetime.month_names.to_json},
+ monthNamesShort: #{raw RailsAdmin::Config::Fields::Types::Datetime.abbr_month_names.to_json},
+ }
+ }
+
= @ordered_filter_string
+
});
%style
- properties.select{ |p| p.column_width.present? }.each do |property|
View
1 config/locales/rails_admin.en.yml
@@ -8,6 +8,7 @@ en:
truncate: ""
admin:
misc:
+ filter_date_format: "mm/dd/yy" # a combination of 'dd', 'mm' and 'yy' with any delimiter. No other interpolation will be done!
search: "Search"
filter: "Filter"
refresh: "Refresh"
View
43 lib/rails_admin/adapters/active_record.rb
@@ -171,29 +171,36 @@ def build_statement(column, type, value, operator)
end
["(#{column} #{LIKE_OPERATOR} ?)", value]
when :datetime, :timestamp, :date
- return unless operator != 'default'
- values = case operator
+
+ 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).yesterday.end_of_day rescue false) : false
+ end_date = value[2].present? ? (Date.strptime(value[2], date_format).end_of_day rescue false) : false
when 'today'
- [Date.today.beginning_of_day, Date.today.end_of_day]
+ start_date = Date.today.yesterday.end_of_day
+ end_date = Date.today.end_of_day
when 'yesterday'
- [Date.yesterday.beginning_of_day, Date.yesterday.end_of_day]
+ start_date = Date.yesterday.yesterday.end_of_day
+ end_date = Date.yesterday.end_of_day
when 'this_week'
- [Date.today.beginning_of_week.beginning_of_day, Date.today.end_of_week.end_of_day]
+ start_date = Date.today.beginning_of_week.yesterday.end_of_day
+ end_date = 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?
- return ["(#{column} >= ?)", value.to_i.days.ago]
- when 'more_than'
- return if value.blank?
- 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
+ start_date = 1.week.ago.to_date.beginning_of_week.yesterday.end_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).yesterday.end_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]
+ elsif start_date
+ ["(#{column} >= ?)", start_date]
+ elsif end_date
+ ["(#{column} <= ?)", end_date]
end
- ["(#{column} BETWEEN ? AND ?)", *values]
when :enum
return if value.blank?
["(#{column} IN (?))", Array.wrap(value)]
View
3 spec/integration/basic/list/rails_admin_basic_list_spec.rb
@@ -246,7 +246,8 @@
end
visit index_path(:model_name => "player")
- should have_content("$.filters.append('Name', 'name', 'string', '', '', '', false, '1');$.filters.append('Team', 'team', 'belongs_to_association', '', '', '', false, '2');")
+ should have_content(%{$.filters.append("Name", "name", "string", null, null, "", 1);})
+ should have_content(%{$.filters.append("Team", "team", "belongs_to_association", null, null, "", 2);})
end
end
View
22 spec/unit/adapters/active_record_spec.rb
@@ -132,6 +132,7 @@ class ARComment < ActiveRecord::Base
param[:model_proc].call.should == [ARBlog, ARPost]
end
end
+
describe "#properties" do
before do
@@ -355,4 +356,25 @@ class ARComment < ActiveRecord::Base
RailsAdmin::AbstractModel.new('User').serialized_attributes.keys.should == ["roles"]
end
end
+
+
+ describe '#all' do
+ context 'filters on dates' do
+ it 'lists elements within outbound limits' do
+ 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')
+
+ FieldTest.create!(:date_field => Date.strptime("01/01/2012", date_format))
+ FieldTest.create!(:date_field => Date.strptime("01/02/2012", date_format))
+ FieldTest.create!(:date_field => Date.strptime("01/03/2012", date_format))
+ FieldTest.create!(:date_field => Date.strptime("01/04/2012", date_format))
+ RailsAdmin.config(FieldTest).abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "01/02/2012", "01/03/2012"], :o => 'between' } } } ).count.should == 2
+ RailsAdmin.config(FieldTest).abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "01/02/2012", "01/02/2012"], :o => 'between' } } } ).count.should == 1
+ RailsAdmin.config(FieldTest).abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "01/03/2012", ""], :o => 'between' } } } ).count.should == 2
+ RailsAdmin.config(FieldTest).abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "", "01/02/2012"], :o => 'between' } } } ).count.should == 2
+ RailsAdmin.config(FieldTest).abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["01/02/2012"], :o => 'default' } } } ).count.should == 1
+
+ end
+ end
+ end
+
end

0 comments on commit abbd06c

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