Skip to content

Instantly share code, notes, and snippets.

@mustmodify
Last active August 29, 2015 14:06
Show Gist options
  • Save mustmodify/0ae75ff9d79fe8cc3e1b to your computer and use it in GitHub Desktop.
Save mustmodify/0ae75ff9d79fe8cc3e1b to your computer and use it in GitHub Desktop.
class UserSearch < Valuable
has_value :term
has_value :include_inactive, :default => false
has_value :postal_code
def scope
User.apply( conditions )
end
def term_like
"#{term}%".sanitize
end
def conditions
[].tap do |out|
out << "first_name LIKE #{term_like} OR ... " unless term.blank? # fear of injection attach
out << "active = 1" unless include_inactive? # SQL implementation specific. :(
out << "postal_code = #{self.postal_code.sanitize}" if postal_code.not.blank?
end
end
end
class UserSearch < Valuable
has_value :term
has_value :include_inactive, :default => false
has_value :postal_code
def scope
User.apply( conditions )
end
def conditions
[].tap do |out|
out << lambda { where('first_name LIKE ":term%" OR last_name LIKE ":term%" OR username LIKE ":term%"', term: self.term) } unless term.blank?
out << lambda { where(active: true) } unless include_inactive?
out << lambda { where(postal_code: self.postal_code) } if postal_code.not.blank?
end
end
end
class UserSearch < Valuable
has_value :term
has_value :include_inactive, :default => false
has_value :postal_code
def scope
User.apply( conditions )
end
def conditions
scope = User.scope
scope = scope.where('first_name LIKE ":term%" OR last_name LIKE ":term%" OR username LIKE ":term%"', term: self.term) } unless term.blank?
scope = scope.where(active: true) } unless include_inactive?
scope = scope.where(postal_code: self.postal_code) } if postal_code.not.blank?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment