Module: Iord::Search

Extended by:
ActiveSupport::Concern
Defined in:
lib/iord/search.rb

Instance Method Summary collapse

Instance Method Details

#create_collection_with_searchObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/iord/search.rb', line 112

def create_collection_with_search
  collection = create_collection_without_search
  if search_term
    if search_operator == :like
      collection = collection.where(search_term => {"$regex" => ".*#{search_value}.*", "$options" => "i"} )
    elsif search_operator == :eq
      if search_value.empty?
        collection = collection.where(search_term.to_sym.in => [nil, ''])
      else
        type = term_type(resource_class, search_term)
        collection = collection.where(search_term => type.evolve(search_value))
      end
    else
      collection = collection.where(search_term => {"$#{search_operator}" => search_value})
    end
  end
  return collection
end

#search_operatorObject



101
102
103
104
105
106
107
108
109
110
# File 'lib/iord/search.rb', line 101

def search_operator
  if @search_operator.nil? or (params[:op] ? @search_operator != params[:op].to_s : false)
    return @search_operator = nil if search_term.nil?
    @search_operator = (params[:op] || :eq).to_sym
    @search_operator = :eq unless search_operators.include? @search_operator

    collection_url_defaults[:op] = @search_operator if @search_operator
  end
  return @search_operator
end

#search_operatorsObject



97
98
99
# File 'lib/iord/search.rb', line 97

def search_operators
  @search_operators ||= %i(eq lt lte gt gte like)
end

#search_termObject



69
70
71
72
73
74
75
76
# File 'lib/iord/search.rb', line 69

def search_term
  if @search_term.nil?
    @search_term = params[:q]
    @search_term = nil unless resource_attribute_names.include? @search_term
    collection_url_defaults[:q] = @search_term if @search_term
  end
  return @search_term
end

#search_valueObject



87
88
89
90
91
92
93
94
95
# File 'lib/iord/search.rb', line 87

def search_value
  if @search_value.nil?
    return @search_value = nil if search_term.nil?
    @search_value = params[:v]

    collection_url_defaults[:v] = @search_value if @search_value
  end
  return @search_value
end

#term_type(klass, term) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/iord/search.rb', line 78

def term_type(klass, term)
  term = term.split('.') if term.is_a?(String)
  if term.count == 1
    klass.fields[term[0]].type
  else
    term_type(klass.relations[term[0]].klass, term[1..-1])
  end
end