Skip to content

Instantly share code, notes, and snippets.

@Arkham
Created May 28, 2013 09:09
Show Gist options
  • Save Arkham/5661524 to your computer and use it in GitHub Desktop.
Save Arkham/5661524 to your computer and use it in GitHub Desktop.
class SearchPlaces
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :lat, :lng, :distance, :address
attr_accessor :query
attr_accessor :type_id
attr_accessor :category_id
def initialize(attributes)
attributes ||= {}
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
def scope
scope = Place.published
if lat && lng && distance
scope = scope.near([lat, lng], distance)
end
if query.present?
scope = scope.matching(query)
end
if type_id.present?
scope = scope.of_type(type_id)
end
if category_id.present?
scope = scope.of_category(category_id)
end
scope
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment