Skip to content

Commit

Permalink
split equal into array and non array methods
Browse files Browse the repository at this point in the history
  • Loading branch information
James Vanneman committed Feb 5, 2013
1 parent da30cbe commit 5c23e13
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 17 deletions.
7 changes: 3 additions & 4 deletions lib/searchlogic/conditions/_condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ def initialize(klass, method_name, args, &block)
@block = block
end

private
def applicable?
raise NotImplementedError.new("You need to define a #applicable method")
end
def applicable?
raise NotImplementedError.new("You need to define a #applicable method")
end
end
end
end
9 changes: 6 additions & 3 deletions lib/searchlogic/conditions/equals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ module Conditions
class Equals < Condition

def scope
returned_objects = values.map {|value| klass.where("#{table_name}.#{column_name} = ?", value)}.flatten if applicable?
if values.kind_of?(Array)
returned_objects = values.map {|value| klass.where("#{table_name}.#{column_name} = ?", value)}.flatten if applicable?
else
klass.where("#{table_name}.#{column_name} = ?", values) if applicable?
end
end

private
def values
[args.first].flatten
args.first
end

def applicable?
!(/^(#{klass.column_names.join("|")})_equals$/ =~ method_name).nil? if klass
end
Expand Down
8 changes: 4 additions & 4 deletions spec/searchlogic/begins_with_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

describe Searchlogic::Conditions::BeginsWith do
before(:each) do
@james = User.new
@james.name = "James"
@james.save
@james = User.create(:name=>"James", :age=>26)
end

it "should not match middle of work" do
User.name_begins_with("am").should be_empty
end
it "finds user based on beginning" do
User.name_begins_with("Jam").should_not be_empty
users = User.name_begins_with("Jam")
users.count.should eq(1)
users.first.name.should eq("James")
end
end
15 changes: 11 additions & 4 deletions spec/searchlogic/equals_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe Searchlogic::Conditions::Equals do
before(:each) do
@james = User.create(:name=>"James")
@james = User.create(:name=>"James", :age=>28)
@ben = User.create(:name=> "Ben")
end

Expand All @@ -20,8 +20,15 @@
names.count.should eq(2)
names.should eq(["Ben","James"])
end
xit "and raises NoMethodError when column doesn't exist" do
User.titties_equals("Big").should_raise NoMethodError
end
end

it "can be chained with other scopes" do
james = User.create(:name=>"James", :age=>26)
p User.age_equals("James").class
users = User.name_equals("James").age_equals(28)
users.count.should eq(1)
users.first.name.should eq("James")
users.first.age.should eq(28)
end

end
17 changes: 17 additions & 0 deletions spec/searchlogic/joins_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'spec_helper'

describe "JOINS" do
before(:each) do
@james = User.create(:name=>"James")
@ben = User.create(:name=>"Ben")
end

it "finds all users with null name" do
no_name = User.new
no_name.name = nil
no_name.save
no_name_id = no_name.id
find_users = User.name_null.map { |u| u.id }
find_users.should eq([no_name_id])
end
end
107 changes: 105 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,29 @@
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
ActiveRecord::Base.configurations = true
ActiveRecord::Schema.verbose = false
ActiveRecord::Schema.define(:version => 3) do
ActiveRecord::Schema.define(:version => 1) do
create_table :audits do |t|
t.string :auditable_type
t.integer :auditable_id
end

create_table :companies do |t|
t.datetime :created_at
t.datetime :updated_at
t.string :name
t.string :description
t.integer :users_count, :default => 0
end

create_table :user_groups do |t|
t.string :name
end

create_table :user_groups_users, :id => false do |t|
t.integer :user_group_id, :null => false
t.integer :user_id, :null => false
end

create_table :users do |t|
t.datetime :created_at
t.datetime :updated_at
Expand All @@ -15,9 +37,89 @@
t.string :some_type_id
t.datetime :whatever_at
end

create_table :carts do |t|
t.datetime :created_at
t.datetime :updated_at
t.integer :user_id
end

create_table :orders do |t|
t.datetime :created_at
t.datetime :updated_at
t.integer :user_id
t.date :shipped_on
t.float :taxes
t.float :total
end

create_table :fees do |t|
t.datetime :created_at
t.datetime :updated_at
t.string :owner_type
t.integer :owner_id
t.float :cost
end

create_table :line_items do |t|
t.datetime :created_at
t.datetime :updated_at
t.integer :order_id
t.float :price
end
end


Spec::Runner.configure do |config|
config.before(:each) do
class ::Audit < ActiveRecord::Base
belongs_to :auditable, :polymorphic => true
end

class ::Company < ActiveRecord::Base
has_many :orders, :through => :users
has_many :users, :dependent => :destroy
end

class ::Cart < ActiveRecord::Base
belongs_to :user
end

class ::UserGroup < ActiveRecord::Base
has_and_belongs_to_many :users
end

class ::User < ActiveRecord::Base
belongs_to :company, :counter_cache => true
has_many :carts, :dependent => :destroy
has_many :orders, :dependent => :destroy
has_many :orders_big, :class_name => 'Order', :conditions => 'total > 100'
has_many :audits, :as => :auditable
has_and_belongs_to_many :user_groups

self.skip_time_zone_conversion_for_attributes = [:whatever_at]
end

class ::Order < ActiveRecord::Base
belongs_to :user
has_many :line_items, :dependent => :destroy
end

class ::Fee < ActiveRecord::Base
belongs_to :owner, :polymorphic => true
end

class ::LineItem < ActiveRecord::Base
belongs_to :order
end

::Company.destroy_all
::User.destroy_all
::Order.destroy_all
::LineItem.destroy_all
end
end

Spec::Runner.configure do |config|
class ::User < ActiveRecord::Base
belongs_to :company, :counter_cache => true
Expand All @@ -41,4 +143,5 @@ class ::User < ActiveRecord::Base
DatabaseCleaner.clean
end

end
end

0 comments on commit 5c23e13

Please sign in to comment.