Permalink
Browse files

Added the possibility to filter by date ranges.

For example to be able to filter games by a range of dates in the
"valid_until" field, something like this has to be added to the
configuration file:

Game:
  fields:
	options:
	  filter_by_date_range: valid_until

Signed-off-by: Francesc Esplugas <francesc@intraducibles.com>
  • Loading branch information...
Gaston Kleiman Francesc Esplugas
Gaston Kleiman authored and Francesc Esplugas committed Feb 10, 2010
1 parent 2d697bf commit 3bb6f53e71469fe49d235e6220b54cd17acd6ecb
@@ -191,17 +191,29 @@ def relationship_filter(request, filter, habtm = false)
end
def date_filter(request, filter)
- items = %w( today last_few_days last_7_days last_30_days ).map do |timeline|
- switch = request.include?("#{filter}=#{timeline}") ? 'on' : 'off'
- if switch == 'on'
- options = { :page => nil }
- params.delete(filter)
- else
- options = { filter.to_sym => timeline, :page => nil }
+
+ if !@resource[:class].typus_field_options_for(:filter_by_date_range).include?(filter.to_sym)
+ items = %w( today last_few_days last_7_days last_30_days ).map do |timeline|
+ switch = request.include?("#{filter}=#{timeline}") ? 'on' : 'off'
+ if switch == 'on'
+ options = { :page => nil }
+ params.delete(filter)
+ else
+ options = { filter.to_sym => timeline, :page => nil }
+ end
+ link_to _(timeline.humanize), params.merge(options), :class => switch
end
- link_to _(timeline.humanize), params.merge(options), :class => switch
- end
- build_typus_list(items, :attribute => filter)
+ build_typus_list(items, :attribute => filter)
+ else
+ date_params = params.dup
+
+ %w( action controller page id ).each { |p| date_params.delete(p) }
+ date_params.delete(filter)
+
+ hidden_params = date_params.map { |k, v| hidden_field_tag(k, v) }
+ render "admin/helpers/date", :hidden_params => hidden_params, :filter => filter, :resource => @resource
+ end
+
end
def boolean_filter(request, filter)
@@ -0,0 +1,24 @@
+<%
+ if params[filter.to_s].is_a?(Hash)
+ date_from = params[filter.to_s]["from"]
+ date_to = params[filter.to_s]["to"]
+ else
+ date_from = ""
+ date_to = ""
+ end
+%>
+
+<h2><%= @resource[:class].human_attribute_name(filter) %></h2>
+<ul>
+<% form_tag url_for(:controller => params[:controller]), :method => :get do %>
+ <li>
+ <%= text_field_tag "#{filter}[from]", date_from, :size => 10, :class => :date_input %>
+ -
+ <%= text_field_tag "#{filter}[to]", date_to, :size => 10, :class => :date_input %>
+ </li>
+ <li>
+ <%= hidden_params.sort.join("\n").html_safe! %>
+ <%= submit_tag _("Filter") %>
+ </li>
+<% end %>
+</ul>
@@ -225,6 +225,12 @@ input#search { border: 1px solid #999; font-size: 1.2em; padding: 3px; }
/* @end */
+/* @group Date search boxes */
+
+input.date_input { border: 1px solid #999; margin-bottom: 1em;}
+
+/* @end */
+
/* @group Login Page Dialog */
#bottom_dialog {
View
@@ -249,18 +249,26 @@ def build_conditions(params)
condition = ["#{key} BETWEEN ? AND ?", interval.first.to_s(:db), interval.last.to_s(:db)]
conditions = merge_conditions(conditions, condition)
when :date
- interval = case value
- when 'today' then nil
- when 'last_few_days' then 3.days.ago.to_date..Date.tomorrow
- when 'last_7_days' then 6.days.ago.midnight..Date.tomorrow
- when 'last_30_days' then (Date.today << 1)..Date.tomorrow
- end
- if interval
- condition = ["#{key} BETWEEN ? AND ?", interval.first.to_s, interval.last.to_s]
- elsif value == 'today'
- condition = ["#{key} = ?", Date.today.to_s]
+ if value.is_a?(Hash)
+ begin
+ conditions = merge_conditions(conditions, ["#{key} >= ?", Date.parse(value["from"])]) unless value["from"].blank?
+ conditions = merge_conditions(conditions, ["#{key} <= ?", Date.parse(value["to"])]) unless value["to"].blank?
+ rescue
+ end
+ else
+ interval = case value
+ when 'today' then nil
+ when 'last_few_days' then 3.days.ago.to_date..Date.tomorrow
+ when 'last_7_days' then 6.days.ago.midnight..Date.tomorrow
+ when 'last_30_days' then (Date.today << 1)..Date.tomorrow
+ end
+ if interval
+ condition = ["#{key} BETWEEN ? AND ?", interval.first.to_s, interval.last.to_s]
+ elsif value == 'today'
+ condition = ["#{key} = ?", Date.today.to_s]
+ end
+ conditions = merge_conditions(conditions, condition)
end
- conditions = merge_conditions(conditions, condition)
when :integer, :string
condition = { key => value }
conditions = merge_conditions(conditions, condition)
View
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Francesc Esplugas"]
- s.date = %q{2010-02-06}
+ s.date = %q{2010-02-10}
s.description = %q{Effortless backend interface for Ruby on Rails applications. (Admin scaffold generator.)}
s.email = %q{francesc@intraducibles.com}
s.extra_rdoc_files = [
@@ -35,6 +35,7 @@ Gem::Specification.new do |s|
"app/models/typus_user.rb",
"app/views/admin/dashboard/_sidebar.html.erb",
"app/views/admin/helpers/_applications.html.erb",
+ "app/views/admin/helpers/_date.html.erb",
"app/views/admin/helpers/_display_link_to_previous.html.erb",
"app/views/admin/helpers/_flash_message.html.erb",
"app/views/admin/helpers/_header.html.erb",

0 comments on commit 3bb6f53

Please sign in to comment.