Class: SearchPage

Inherits:
Page show all
Defined in:
lib/yodel/models/search/search_page.rb

Constant Summary collapse

OPERATOR_METHODS =
{
  'Equals' => nil,
  'Not Equal' => :ne,
  'Greater Than' => :gt,
  'Less Than' => :lt,
  'Greater Than or Equal To' => :gte,
  'Less Than or Equal To' => :lte,
  'In' => :in
}

Constants included from Authentication

Authentication::AUTHORIZATION_KEYS

Constants inherited from AbstractRecord

AbstractRecord::CALLBACKS, AbstractRecord::FIELD_CALLBACKS, AbstractRecord::ORDERS

Instance Attribute Summary

Attributes inherited from Record

#mixins, #model, #model_record, #real_record

Attributes inherited from SiteRecord

#site

Attributes inherited from AbstractRecord

#changed, #errors, #stash, #typecast, #values

Instance Method Summary collapse

Methods inherited from Page

#assign_child_paths, #assign_path, #assign_permalink, #content, #content_for, #env, #flash, #form_for, #layout, #menu, #mime_type, #mime_type=, #page, #params, #partial, #render_layout, #render_or_default, #request, #request=, respond_to, #respond_to_request, #response, #response=, #session, #set_content, #snippet, #status, #user_allowed_to?, with

Methods included from HTMLDecorator

#delete_button, #delete_link, #form_for_page, #immediately, #on_click, #paragraph, #paragraphs_from

Methods included from Authentication

#current_user, #logged_in?, #login, #logout, #prompt_login, #store_authenticated_user

Methods inherited from Record

#all_children, #append_to_siblings, #children_and_self, #collection, #content, #create_eigenmodel, #create_mixin_instances, #default_values, #delegate_mixins, #destroy_children, #field_sections, #fields, #first_non_blank_response_to, #first_parent, #first_response_to, #get_binding, #has_eigenmodel?, #initialize, #insert_in_siblings, #inspect_hash, #load_model, #model_name, #parent?, #parents, #perform_reload, #prepare_reload_params, #remove_eigenmodel, #remove_from_siblings, #root?, #run_record_after_create_callbacks, #run_record_after_destroy_callbacks, #run_record_after_save_callbacks, #run_record_after_update_callbacks, #run_record_after_validation_callbacks, #run_record_before_create_callbacks, #run_record_before_destroy_callbacks, #run_record_before_save_callbacks, #run_record_before_update_callbacks, #run_record_before_validation_callbacks, #set_content, #siblings, #to_str, #update_search_keywords, #user_allowed_to?, #user_allowed_to_create?, #user_allowed_to_delete?, #user_allowed_to_update?, #user_allowed_to_view?

Methods inherited from SiteRecord

#default_values, #initialize, #inspect_hash, #perform_reload, #prepare_reload_params, #site_id

Methods included from SiteModel

#load, #scoped, #scoped_for

Methods included from MongoModel

#collection, #load, #scoped

Methods included from AbstractModel

#embed_many, #embed_one, #field, #fields, #many, #modify_field, #one, #remove_field

Methods inherited from MongoRecord

#collection, #default_values, #fields, #id, #increment!, #inspect_hash, #load_from_mongo, #load_mongo_document, #perform_destroy, #perform_reload, #perform_save, #set_id

Methods inherited from AbstractRecord

#changed!, #changed?, #clear_key, #default_values, #destroy, #destroyed?, #eql?, #errors?, #field, #field?, #field_was, #fields, #from_json, #get, #get_meta, #get_raw, #hash, #id, #increment!, inherited, #initialize, #inspect, #inspect_hash, #inspect_value, #method_missing, #new?, #prepare_reload_params, #present?, #reload, #save, #save_without_validation, #search_terms, #set, #set_meta, #set_raw, #to_json, #to_str, #trigger_field_callback, #update, #valid?

Constructor Details

This class inherits a constructor from Record

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class AbstractRecord

Instance Method Details

#add_condition(q, type, field, operator, value) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/yodel/models/search/search_page.rb', line 36

def add_condition(q, type, field, operator, value)
  return q if value.blank?
  operator = OPERATOR_METHODS[operator]
  field = field.to_sym
  
  if type && type.all_record_fields[field.to_s]
    value = type.all_record_fields[field.to_s].from_json(value, type)
  end
  
  if operator
    q.where(field.send(operator) => value)
  else
    q.where(field => value)
  end
end

#queryObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/yodel/models/search/search_page.rb', line 12

def query
  @query ||= begin
    query_type = type || site.records
    q = query_type.where(show_in_search: true)
  
    # constant constraints
    conditions.each do |condition|
      q = add_condition(q, query_type, condition.name, condition.operator, condition.value)
    end
  
    # user conditions
    user_conditions.each do |condition|
      param_name = condition.as || condition.name
      q = add_condition(q, query_type, condition.name, condition.operator, params[param_name])
    end
  
    # add other optional search parameters
    q = q.sort(sort || params['sort']) if sort || params['sort']
    q = q.limit(limit || params['limit'].to_i) if limit || params['limit']
    q = q.skip(skip || params['skip'].to_i) if skip || params['skip']
    q
  end
end

#user_query?Boolean

True if the request contains any of the parameters

Returns:

  • (Boolean)


53
54
55
# File 'lib/yodel/models/search/search_page.rb', line 53

def user_query?
  user_conditions.any? {|condition| params[condition.as ? condition.as : condition.name].present?}
end