Skip to content

Commit

Permalink
fixing errors after refactoring ScopeReflection
Browse files Browse the repository at this point in the history
  • Loading branch information
James Vanneman committed Mar 20, 2013
1 parent 3f1ebba commit 49d127d
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 95 deletions.
Expand Up @@ -22,13 +22,13 @@ def self.matcher
def convert_syntax(method, for_klass)
m = method.to_s.split(DELIMITER)
meth = method
if !!(incorrect_syntax(for_klass).match(meth)) && !(preference_to_columns?(m.first.singularize.camelize.constantize, m.last) rescue nil)
if !!(incorrect_syntax(for_klass).match(meth)) && !(preference_to_column_or_scope?(m.first.singularize.camelize.constantize, m.last) rescue nil)
syntax_error = meth.to_s.scan(incorrect_syntax(for_klass)).flatten.first
associated_klass = syntax_error.gsub(/_$/, "").singularize.camelize.constantize
converted_method = meth.to_s.gsub(syntax_error, syntax_error + "_")
if incorrect_syntax(associated_klass) =~ converted_method && !(preference_to_columns?(associated_klass, converted_method.split(DELIMITER).last))
if incorrect_syntax(associated_klass) =~ converted_method && !(preference_to_column_or_scope?(associated_klass, converted_method.split(DELIMITER).last))
convert_syntax(converted_method, associated_klass)
elsif preference_to_columns?(associated_klass, converted_method.split(DELIMITER).last)
elsif preference_to_column_or_scope?(associated_klass, converted_method.split(DELIMITER).last)
self.converted_method = converted_method
else
self.converted_method = converted_method unless incorrect_syntax(associated_klass) =~ converted_method
Expand All @@ -39,16 +39,16 @@ def convert_syntax(method, for_klass)
end

def applicable?
/(#{(ActiveRecord::Base.connection.tables + ActiveRecord::Base.connection.tables.map(&:singularize)).join("|")})_[^_]/ =~ method_name && !(preference_to_columns?(klass, method_name))
/(#{(ActiveRecord::Base.connection.tables + ActiveRecord::Base.connection.tables.map(&:singularize)).join("|")})_[^_]/ =~ method_name && !(preference_to_column_or_scope?(klass, method_name))
end

def incorrect_syntax(match_klass)
/(#{matching_incorrect_syntax(match_klass)})[^_]/
end

def preference_to_columns?(for_klass, method)
##give preference to columns if method starts with col_nam_known_scope or known_scope_col_name
/^(#{for_klass.column_names.join("|")})(#{ScopeReflection.all_scopes.join("|")})/ =~ method || /^(ascend_by_|descend_by_)(#{for_klass.column_names.join("|")})/ =~ method_name
def preference_to_column_or_scope?(for_klass, method)
scope_refl = ScopeReflection.new(method, for_klass)
scope_refl.column? && scope_refl.scope?
end

def matching_incorrect_syntax(match_klass)
Expand Down
10 changes: 3 additions & 7 deletions lib/searchlogic/active_record_ext/scopes/conditions/oor.rb
Expand Up @@ -27,20 +27,16 @@ def self.matcher
private

def send_and_store(m)
scope_key = ScopeReflection.scope_name(m)
if no_arg_scope?(scope_key)
method = ScopeReflection.new(m)
if method.scope_lambda.try(:arity) == 0
scope = klass.__send__(m)
store_values(scope)
else
[value].flatten.size == 1 ? scope = klass.__send__(m, value) : scope = klass.__send__(m, *value)
scope = [value].flatten.size == 1 ? klass.__send__(m, value) : klass.__send__(m, *value)
end
store_values(scope)
end

def no_arg_scope?(scope_key)
!!(ScopeReflection.all_named_scopes_hash[scope_key].try(:[], :scope).try(:arity) == 0)
end

def store_values(scope)
joins_values << scope.joins_values
wv = scope.where_values
Expand Down

This file was deleted.

@@ -0,0 +1,35 @@
module Searchlogic
module ActiveRecordExt
module Scopes
module Conditions
class MethodConstructor
attr_reader :method_name, :method_parts
def initialize(method_name)
@method_name = method_name
@method_parts = method_name.to_s.split(/_or_(?!equal)/)
end

def methods_array
method_parts.map { |m| add_condition(m) }
end

private

def ending_alias_condition
@ending_alias_condition ||= ScopeReflection.new(method_name).predicate
end

def add_condition(method)
return method if /(_any|_all)$/ =~ method
if ScopeReflection.authorized?(method)
grouping = /(_any|_all)$/.match(ending_alias_condition)
grouping.nil? ? method : method + grouping
else
method + ending_alias_condition
end
end
end
end
end
end
end
15 changes: 15 additions & 0 deletions lib/searchlogic/scope_reflection_ext/instance_methods.rb
Expand Up @@ -25,7 +25,22 @@ def predicate
msg = "`#{method}' is not defined in searchlogic"
raise NoMethodError.new(msg)
end
end

def column?
begin
/^(#{klass.column_names.join("|")})/ =~ method || /^(ascend_by_|descend_by_)(#{klass.column_names.join("|")})/ =~ method
rescue NoMethodError
raise UninitializedClassError.new
end
end

def scope?
begin
/^(#{klass.named_scopes.keys.join("|")})/ =~ method || /^(ascend_by_|descend_by_)(#{klass.named_scopes.keys.join("|")})/ =~ method
rescue
raise UninitializedClassError.new
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/searchlogic/scope_reflection_ext/scope_lambda.rb
Expand Up @@ -3,15 +3,15 @@ module ScopeReflectionExt
module ScopeLambda

def scope_lambda
if all_named_scopes_hash.keys.include?(method)
if all_named_scopes.include?(method)
all_named_scopes_hash[method.to_sym][:scope]
else
nil
end
end

def scope_lambda_type
scope_lambda ? all_named_scopes_hash[method.to_sym][:type] : nil
all_named_scopes.include?(method) ? all_named_scopes_hash[method.to_sym][:type] : nil
end
end
end
Expand Down
Expand Up @@ -7,10 +7,6 @@ def searchlogic_methods
#Searchlogic::ActiveRecordExt::Scopes::Conditions::Condition.all_matchers
ActiveRecord::Base.all_matchers
end

def all_scopes
searchlogic_methods + aliases + all_named_scopes.map(&:to_s)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/searchlogic/scope_reflection_ext/type.rb
Expand Up @@ -14,7 +14,7 @@ def name

def type
if named_scope?
all_named_scopes_hash[condition][:type]
all_named_scopes_hash[scope_name][:type]
elsif boolean_matcher?
:boolean
elsif association_method = klass.association_in_method(method)
Expand Down
10 changes: 10 additions & 0 deletions lib/searchlogic/scope_reflection_ext/uninitialized_class_error.rb
@@ -0,0 +1,10 @@
module Searchlogic
module ScopeReflectionExt
class UninitializedClassError < StandardError
def initialize
msg = "You must initialize ScopeReflection with a class in order to call this method"
super(msg)
end
end
end
end
4 changes: 2 additions & 2 deletions lib/searchlogic/search_ext/delegate.rb
Expand Up @@ -17,15 +17,15 @@ def create_scope(curr_scope, condition, value)
scope_reflection = ScopeReflection.new(condition)
if scope_reflection.scope_lambda_type == :boolean && value == true || ordering?(condition)
curr_scope.__send__(scope_reflection.condition)
elsif scope_reflection.scope_lambda.try(:[], :scope).try(:arity) == 1
elsif scope_reflection.scope_lambda.try(:arity) == 1
curr_scope.__send__(scope_reflection.condition, value)
else
[value].flatten.size == 1 ? curr_scope.__send__(scope_reflection.condition, value) : curr_scope.__send__(scope_reflection.condition, *value)
end
end

def false_scope_proc?(key, value)
ScopeReflection.new(key).scope_lambda && !value
ScopeReflection.new(key).named_scope? && !value
end
end
end
Expand Down
16 changes: 3 additions & 13 deletions spec/searchlogic/alias_spec.rb
Expand Up @@ -14,8 +14,8 @@
Searchlogic::Alias.convert_alias(:username_gte).should eq("username_greater_than_or_equal_to")
end

it "returns nil if it doesn't find alias in the method" do
Searchlogic::Alias.convert_alias(:username_greater_than_or_equal_to).should be_nil
it "returns the method" do
Searchlogic::Alias.convert_alias(:username_greater_than_or_equal_to).should eq(:username_greater_than_or_equal_to)
end

end
Expand All @@ -29,16 +29,6 @@
@tren = User.create(:name=>"Tren", :age =>45)
end

context ".searchlogic_methods" do
xit "should return an array of defined searchlogic method matchers" do

end
end

context ".match_alias" do


end

describe "works with OR conditionals" do
it "with two of the same conditionals" do
Expand Down Expand Up @@ -79,7 +69,7 @@

describe ".convert_alias" do
it "matches ordering" do
Searchlogic::ScopeReflection.convert_alias(User, :method => :order, :value => :ascend_by_id).should eq(:ascend_by_id)
Searchlogic::Alias.convert_alias(:ascend_by_id).should eq(:ascend_by_id)
end

it "is == equals" do
Expand Down
10 changes: 5 additions & 5 deletions spec/searchlogic/scope_reflection/named_scope_methods_spec.rb
Expand Up @@ -16,10 +16,10 @@ class Order; scope :early, lambda{created_at_before(Date.today)};end
context "#named_scope?" do
it "returns true for defined named scopes" do
class User; scope :young, lambda{};end
Searchlogic::ScopeReflection.named_scope?(:young).should be_true
Searchlogic::ScopeReflection.new(:young).named_scope?.should be_true
end
it "returns false for undefined scopes" do
Searchlogic::ScopeReflection.named_scope?(:undefined).should be_false
Searchlogic::ScopeReflection.new(:undefined).named_scope?.should be_false
end
end

Expand All @@ -36,16 +36,16 @@ class Company; scope :comp, lambda{|price| orders_line_items_price_eq(price)};en
it "returns the name of the scope in the method on an association" do
class User; scope :fool, lambda{|age| age_gte(age)};end
class Company; scope :comp, lambda{|price| orders_line_items_price_eq(price)};end
Searchlogic::ScopeReflection.scope_name(:users_fool).should eq(:fool)
Searchlogic::ScopeReflection.new(:users_fool).scope_name.should eq(:fool)
end

it "returns scope name" do
class User; scope :fool, lambda{|age| age_gte(age)};end
Searchlogic::ScopeReflection.scope_name(:fool).should eq(:fool)
Searchlogic::ScopeReflection.new(:fool).scope_name.should eq(:fool)
end

it "return nil if no scope exists" do
Searchlogic::ScopeReflection.scope_name(:foool).should be_nil
Searchlogic::ScopeReflection.new(:foool).scope_name.should be_nil
end
end

Expand Down
30 changes: 29 additions & 1 deletion spec/searchlogic/scope_reflection/scope_reflection_spec.rb
Expand Up @@ -8,8 +8,36 @@
end

it "should raise an Error if the class doesn't exist" do
expect{Searchlogic::ScopeReflection.new(:name_equal, User)}.to raise_error
expect{Searchlogic::ScopeReflection.new(:name_equal, Usser)}.to raise_error
end
end

context "#column?" do
it "should return true if it's a column of the class" do
Searchlogic::ScopeReflection.new(:users_count, Company).column?.should be_true
end

it "should raise an error if ScopeReflection was not initialized with a class" do
expect{Searchlogic::ScopeReflection.new(:users_count).column?}.to raise_error Searchlogic::ScopeReflectionExt::UninitializedClassError

end
end
context "#scope?" do
it "should return true if it's a scope of the class" do
class Company; scope :company_scope, lambda{};end
Searchlogic::ScopeReflection.new(:company_scope, Company).scope?.should be_true
end

it "should return false if it's a scope of another class" do
class User; scope :user_scope, lambda{};end
Searchlogic::ScopeReflection.new(:user_scope, Company).scope?.should be_false
end

it "should raise an error if ScopeReflection was not initialized with a class" do
class User; scope :user_scope, lambda{};end

expect{Searchlogic::ScopeReflection.new(:users_count).scope?}.to raise_error Searchlogic::ScopeReflectionExt::UninitializedClassError

end
end
end

0 comments on commit 49d127d

Please sign in to comment.