Skip to content

Commit

Permalink
Refactoring of AdvancedSearchController:
Browse files Browse the repository at this point in the history
- Employs ActiveSupport's Object#present? method over AdvancedSearchController#nil_or_empty
- Cleans up code formatting a little
  • Loading branch information
dchapman1988 committed Dec 12, 2013
1 parent fc0097b commit 009b70b
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions app/controllers/advanced_search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,51 +56,48 @@ def add_search_filters params

def add_created_by_organisation_filter params
@criteria_list.push(SearchFilter.new({:field => "created_organisation",
:value => params[:created_by_organisation_value],
:index => 1,
:join => "AND"})) if !self.class.nil_or_empty(params, :created_by_organisation_value)
:value => params[:created_by_organisation_value],
:index => 1,
:join => "AND"})) if params[:created_by_organisation_value].present?
end

def add_created_by_filter params
@criteria_list.push(SearchFilter.new({:field => "created_by",
:field2 => "created_by_full_name",
:value => params[:created_by_value],
:index => 1,
:join => "AND"})) if !self.class.nil_or_empty(params, :created_by_value)
:field2 => "created_by_full_name",
:value => params[:created_by_value],
:index => 1,
:join => "AND"})) if params[:created_by_value].present?
end

def add_updated_by_filter params
@criteria_list.push(SearchFilter.new({:field => "last_updated_by",
:field2 => "last_updated_by_full_name",
:value => params[:updated_by_value],
:index => 2,
:join => "AND"})) if !self.class.nil_or_empty(params, :updated_by_value)
:field2 => "last_updated_by_full_name",
:value => params[:updated_by_value],
:index => 2,
:join => "AND"})) if params[:updated_by_value].present?
end

def add_created_at_filter params
@criteria_list.push(SearchDateFilter.new({:field => "created_at",
:from_value => (self.class.nil_or_empty(params, :created_at_after_value) ? "*" : "#{params[:created_at_after_value]}T00:00:00Z"),
:to_value => (self.class.nil_or_empty(params, :created_at_before_value) ? "*" : "#{params[:created_at_before_value]}T00:00:00Z"),
:index => 1,
:join => "AND"})) if (!self.class.nil_or_empty(params, :created_at_after_value) || !self.class.nil_or_empty(params, :created_at_before_value))
:from_value => (params[:created_at_after_value].present? ? "*" : "#{params[:created_at_after_value]}T00:00:00Z"),
:to_value => (params[:created_at_before_value].present? ? "*" : "#{params[:created_at_before_value]}T00:00:00Z"),
:index => 1,
:join => "AND"})) if (params[:created_at_after_value].present? || params[:created_at_before_value].present?)
end

def add_updated_at_filter params
@criteria_list.push(SearchDateFilter.new({:field => "last_updated_at",
:from_value => (self.class.nil_or_empty(params, :updated_at_after_value) ? "*" : "#{params[:updated_at_after_value]}T00:00:00Z"),
:to_value => (self.class.nil_or_empty(params, :updated_at_before_value) ? "*" : "#{params[:updated_at_before_value]}T00:00:00Z"),
:index => 2,
:join => "AND"})) if (!self.class.nil_or_empty(params, :updated_at_after_value) || !self.class.nil_or_empty(params, :updated_at_before_value))
:from_value => (params[:updated_at_after_value].present? ? "*" : "#{params[:updated_at_after_value]}T00:00:00Z"),
:to_value => (params[:updated_at_before_value].present? ? "*" : "#{params[:updated_at_before_value]}T00:00:00Z"),
:index => 2,
:join => "AND"})) if (params[:updated_at_after_value].present? || params[:updated_at_before_value].present?)
end

def add_search_criteria_if_none params
@criteria_list.push(SearchCriteria.new) if !child_fields_selected?(params[:criteria_list])
@criteria_list
end

def self.nil_or_empty params, key
params[key].nil? || params[key].empty?
end

def prepare_params_for_limited_access_user user
params[:created_by_value] = user.user_name
Expand Down

0 comments on commit 009b70b

Please sign in to comment.