Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

Validator for "in" where clause and integration with Rails 4 #112

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
source :rubygems
source 'https://rubygems.org'

# Don't do a "gemspec" here. Seriously. It jacks up Jeweler.

gem "activerecord", "~> 3.1"
gem "activesupport", "~> 3.1"
gem "polyamorous", "~> 0.5.0"
gem "actionpack", "~> 3.1"
gem "activerecord", "~> 4.0.1"
gem "activesupport", "~> 4.0.1"
gem "polyamorous", "~> 0.6.4"
gem "actionpack", "~> 4.0.1"
gem 'rake'

group :development do
gem 'sqlite3'
gem "shoulda", "~> 2.11"
end
gem 'jeweler'
end
2 changes: 1 addition & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ In your controller:

def index
@search = Article.search(params[:search])
@articles = @search.all # load all matching records
@articles = @search.to_a # load all matching records
# @articles = @search.relation # Retrieve the relation, to lazy-load in view
# @articles = @search.paginate(:page => params[:page]) # Who doesn't love will_paginate?
end
Expand Down
8 changes: 4 additions & 4 deletions lib/meta_search/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Builder
# for how it will expose this model and its associations to your controllers/views.
def initialize(base_or_relation, opts = {})
opts = opts.dup
@relation = base_or_relation.scoped
@relation = base_or_relation.all
@base = @relation.klass
@search_key = (opts.delete(:search_key) || 'search').to_s
@options = opts # Let's just hang on to other options for use in authorization blocks
Expand All @@ -51,13 +51,13 @@ def get_association(assoc, base = @base)

def get_attribute(name, parent = @join_dependency.join_base)
attribute = nil
if get_column(name, parent.active_record)
if get_column(name, parent.base_klass)
attribute = parent.table[name]
elsif (segments = name.to_s.split(/_/)).size > 1
remainder = []
found_assoc = nil
while remainder.unshift(segments.pop) && segments.size > 0 && !found_assoc do
if found_assoc = get_association(segments.join('_'), parent.active_record)
if found_assoc = get_association(segments.join('_'), parent.base_klass)
if found_assoc.options[:polymorphic]
unless delimiter = remainder.index('type')
raise PolymorphicAssociationMissingTypeError, "Polymorphic association specified without a type"
Expand All @@ -82,7 +82,7 @@ def get_attribute(name, parent = @join_dependency.join_base)
# MetaSearch::Where
def build(option_hash)
opts = option_hash.dup || {}
@relation = @base.scoped
@relation = @base.all
opts.stringify_keys!
opts = collapse_multiparameter_options(opts)
assign_attributes(opts)
Expand Down
2 changes: 1 addition & 1 deletion lib/meta_search/helpers/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def checks(method, choices = [], options = {}, &block)
#
# Example:
#
# <% f.collection_checks :head_sizes_in, HeadSize.all,
# <% f.collection_checks :head_sizes_in, HeadSize.to_a,
# :id, :name, :class => 'headcheck' do |check| %>
# <%= check.box %> <%= check.label %>
# <% end %>
Expand Down
4 changes: 2 additions & 2 deletions lib/meta_search/helpers/form_helper.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module MetaSearch
module Helpers
module FormHelper
def apply_form_for_options!(object_or_array, options)
def apply_form_for_options!(object_or_array, _record, options)
if object_or_array.is_a?(MetaSearch::Builder)
builder = object_or_array
options[:url] ||= polymorphic_path(object_or_array.base)
elsif object_or_array.is_a?(Array) && (builder = object_or_array.detect {|o| o.is_a?(MetaSearch::Builder)})
options[:url] ||= polymorphic_path(object_or_array.map {|o| o.is_a?(MetaSearch::Builder) ? o.base : o})
else
super
super
return
end

Expand Down
11 changes: 10 additions & 1 deletion lib/meta_search/where.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def create_where_from_args(*args)
unless opts[:formatter].respond_to?(:call)
raise ArgumentError, "Invalid formatter for #{opts[:name]}, should be a Proc or String."
end
opts[:validator] ||= Proc.new {|param| !param.blank?}
opts[:validator] ||= validator_for_name(opts[:name])
unless opts[:validator].respond_to?(:call)
raise ArgumentError, "Invalid validator for #{opts[:name]}, should be a Proc."
end
Expand Down Expand Up @@ -257,6 +257,15 @@ def create_where_compounds_for(where)
)
end
end

def validator_for_name(name)
case name.to_sym
when :in
Proc.new {|param| param.is_a?(Array) && param.reject(&:blank?).present?}
else
Proc.new {|param| !param.blank?}
end
end
end
end

Expand Down
40 changes: 25 additions & 15 deletions meta_search.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-
# stub: meta_search 1.1.3 ruby lib

Gem::Specification.new do |s|
s.name = "meta_search"
s.version = "1.1.3"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Ernie Miller"]
s.date = "2012-02-02"
s.date = "2013-12-25"
s.description = "\n Allows simple search forms to be created against an AR3 model\n and its associations, has useful view helpers for sort links\n and multiparameter fields as well.\n "
s.email = "ernie@metautonomo.us"
s.extra_rdoc_files = [
Expand Down Expand Up @@ -60,31 +61,40 @@ Gem::Specification.new do |s|
s.homepage = "http://metautonomo.us/projects/metasearch/"
s.post_install_message = "\n*** Thanks for installing MetaSearch! ***\nBe sure to check out http://metautonomo.us/projects/metasearch/ for a\nwalkthrough of MetaSearch's features, and click the donate button if\nyou're feeling especially appreciative. It'd help me justify this\n\"open source\" stuff to my lovely wife. :)\n\n"
s.require_paths = ["lib"]
s.rubygems_version = "1.8.15"
s.rubygems_version = "2.2.0.preview.1"
s.summary = "Object-based searching (and more) for simply creating search forms."

if s.respond_to? :specification_version then
s.specification_version = 3
s.specification_version = 4

if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<activerecord>, ["~> 3.1"])
s.add_runtime_dependency(%q<activesupport>, ["~> 3.1"])
s.add_runtime_dependency(%q<polyamorous>, ["~> 0.5.0"])
s.add_runtime_dependency(%q<actionpack>, ["~> 3.1"])
s.add_runtime_dependency(%q<activerecord>, ["~> 4.0.1"])
s.add_runtime_dependency(%q<activesupport>, ["~> 4.0.1"])
s.add_runtime_dependency(%q<polyamorous>, ["~> 0.6.4"])
s.add_runtime_dependency(%q<actionpack>, ["~> 4.0.1"])
s.add_runtime_dependency(%q<rake>, [">= 0"])
s.add_development_dependency(%q<sqlite3>, [">= 0"])
s.add_development_dependency(%q<shoulda>, ["~> 2.11"])
s.add_development_dependency(%q<jeweler>, [">= 0"])
else
s.add_dependency(%q<activerecord>, ["~> 3.1"])
s.add_dependency(%q<activesupport>, ["~> 3.1"])
s.add_dependency(%q<polyamorous>, ["~> 0.5.0"])
s.add_dependency(%q<actionpack>, ["~> 3.1"])
s.add_dependency(%q<activerecord>, ["~> 4.0.1"])
s.add_dependency(%q<activesupport>, ["~> 4.0.1"])
s.add_dependency(%q<polyamorous>, ["~> 0.6.4"])
s.add_dependency(%q<actionpack>, ["~> 4.0.1"])
s.add_dependency(%q<rake>, [">= 0"])
s.add_dependency(%q<sqlite3>, [">= 0"])
s.add_dependency(%q<shoulda>, ["~> 2.11"])
s.add_dependency(%q<jeweler>, [">= 0"])
end
else
s.add_dependency(%q<activerecord>, ["~> 3.1"])
s.add_dependency(%q<activesupport>, ["~> 3.1"])
s.add_dependency(%q<polyamorous>, ["~> 0.5.0"])
s.add_dependency(%q<actionpack>, ["~> 3.1"])
s.add_dependency(%q<activerecord>, ["~> 4.0.1"])
s.add_dependency(%q<activesupport>, ["~> 4.0.1"])
s.add_dependency(%q<polyamorous>, ["~> 0.6.4"])
s.add_dependency(%q<actionpack>, ["~> 4.0.1"])
s.add_dependency(%q<rake>, [">= 0"])
s.add_dependency(%q<sqlite3>, [">= 0"])
s.add_dependency(%q<shoulda>, ["~> 2.11"])
s.add_dependency(%q<jeweler>, [">= 0"])
end
end

2 changes: 1 addition & 1 deletion test/fixtures/company.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Company < ActiveRecord::Base
has_many :developers
has_many :developer_notes, :through => :developers, :source => :notes
has_many :slackers, :class_name => "Developer", :conditions => {:slacker => true}
has_many :slackers, -> { where(:slacker => true) }, :class_name => "Developer"
has_many :notes, :as => :notable
has_many :data_types

Expand Down
1 change: 0 additions & 1 deletion test/fixtures/data_type.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class DataType < ActiveRecord::Base
belongs_to :company
attr_unsearchable :str
attr_protected :str
end
6 changes: 3 additions & 3 deletions test/fixtures/data_types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ dt_<%= n %>:
int : <%= n ** 3 %>
flt : <%= n.to_f / 2.0 %>
dec : <%= n.to_f ** (n + 0.1) %>
dtm : <%= (Time.local(2009, 12, 24) + 86400 * n).in_time_zone.to_s(:db) %>
tms : <%= (Time.local(2009, 12, 24) + 86400 * n).in_time_zone.to_s(:db) %>
tim : <%= Time.local(2000, 01, 01, n+8, n).in_time_zone.to_s(:db) %>
dtm : <%= (Time.utc(2009, 12, 24) + 86400 * n).in_time_zone.to_s(:db) %>
tms : <%= (Time.utc(2009, 12, 24) + 86400 * n).in_time_zone.to_s(:db) %>
tim : <%= Time.utc(2000, 01, 01, n+8, n).in_time_zone.to_s(:db) %>
dat : <%= (Date.new(2009, 12, 24) + n).strftime("%Y-%m-%d") %>
bin : <%= "BLOB#{n}" * n %>
bln : <%= n % 2 > 0 ? true : false %>
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/developer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ class Developer < ActiveRecord::Base
attr_searchable :name, :salary, :if => proc {|s| !s.options[:user] || s.options[:user] == 'privileged'}
assoc_searchable :notes, :projects, :company, :if => proc {|s| !s.options[:user] || s.options[:user] == 'privileged'}

scope :sort_by_salary_and_name_asc, order('salary ASC, name ASC')
scope :sort_by_salary_and_name_desc, order('salary DESC, name DESC')
scope :sort_by_salary_and_name_asc, -> { order('salary ASC, name ASC') }
scope :sort_by_salary_and_name_desc, -> { order('salary DESC, name DESC') }
end
4 changes: 2 additions & 2 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
dep = defined?(ActiveSupport::Dependencies) ? ActiveSupport::Dependencies : ::Dependencies
dep.autoload_paths.unshift FIXTURES_PATH

ActiveRecord::Base.silence do
ActiveRecord::Base.logger.quietly do
ActiveRecord::Migration.verbose = false
load File.join(FIXTURES_PATH, 'schema.rb')
end

ActiveRecord::Fixtures.create_fixtures(FIXTURES_PATH, ActiveRecord::Base.connection.tables)
ActiveRecord::FixtureSet.create_fixtures(FIXTURES_PATH, ActiveRecord::Base.connection.tables)

I18n.load_path += Dir[File.join(File.dirname(__FILE__), 'locales', '*.yml')]

Expand Down
Loading