forked from hzamani/active_record-acts_as
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move query methods override to its place
Fix hzamani#6
- Loading branch information
Showing
4 changed files
with
15 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,25 @@ | ||
|
||
module ActiveRecord | ||
module ActsAs | ||
module Querying | ||
def where(opts = :chain, *rest) | ||
if opts.is_a? Hash | ||
opts, acts_as_opts = opts.partition { |k,v| attribute_names.include?(k.to_s) } | ||
opts, acts_as_opts = Hash[opts], Hash[acts_as_opts] | ||
opts[acting_as_model.table_name] = acts_as_opts | ||
end | ||
super(opts, *rest) | ||
end | ||
|
||
def find_by(*args) | ||
where(*args).take | ||
end | ||
|
||
def find_by!(*args) | ||
where(*args).take! | ||
module QueryMethods | ||
def where_with_acts_as(opts = :chain, *rest) | ||
if acting_as? && opts.is_a?(Hash) | ||
opts, acts_as_opts = opts.stringify_keys.partition { |k,v| attribute_method?(k) } | ||
opts, acts_as_opts = Hash[opts], Hash[acts_as_opts] | ||
opts[acting_as_model.table_name] = acts_as_opts unless acts_as_opts.empty? | ||
end | ||
where_without_acts_as(opts, *rest) | ||
end | ||
alias_method_chain :where, :acts_as | ||
end | ||
|
||
class Relation | ||
alias_method :scope_for_create_without_acting_as, :scope_for_create | ||
|
||
def scope_for_create | ||
def scope_for_create_with_acts_as | ||
@scope_for_create ||= if acting_as? | ||
where_values_hash.merge(where_values_hash(acting_as_model.table_name)).merge(create_with_value) | ||
else | ||
where_values_hash.merge(create_with_value) | ||
end | ||
end | ||
alias_method_chain :scope_for_create, :acts_as | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters