Class: ActiveResourceExtensions::SearchableResource::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/active_resource_extensions/searchable_resource/search.rb

Constant Summary collapse

DEFAULT_PAGE =
1
DEFAULT_PER_PAGE =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, conditions = {}) ⇒ Search

Returns a new instance of Search.



12
13
14
15
# File 'lib/active_resource_extensions/searchable_resource/search.rb', line 12

def initialize model, conditions = {}
  @model = model
  @conditions = conditions
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (protected)

If



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/active_resource_extensions/searchable_resource/search.rb', line 42

def method_missing(name, *args, &block)
  name.to_s =~ /^([\w]*)(=?)$/
  attr = $1.to_sym

  ignore_attribute = ! @model.respond_to?( :is_attribute? )

  if ignore_attribute or @model.is_attribute?( attr )
    if $2 == '='
      @conditions[attr]= args.first
    else
      @conditions[attr]
    end
  else
    super name, *args, &block
  end
end

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



10
11
12
# File 'lib/active_resource_extensions/searchable_resource/search.rb', line 10

def conditions
  @conditions
end

Instance Method Details

#paginate(pagination_options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/active_resource_extensions/searchable_resource/search.rb', line 17

def paginate pagination_options = {}
  # Get the parameters corresponding to the search options
  params = parse_conditions @conditions

  total_entries = @model.count params

  # Manage pagination
  params[:page] = ( pagination_options[:page] || DEFAULT_PAGE ).to_i
  params[:per_page] = ( pagination_options[:per_page] || DEFAULT_PER_PAGE ).to_i

  SearchableResource::Collection.new @model.find( :all, :params => params ), params[:page], params[:per_page], total_entries
end