lib/active_manageable/methods/auxiliary/scopes.rb in active_manageable-0.1.2 vs lib/active_manageable/methods/auxiliary/scopes.rb in active_manageable-0.2.0
- old
+ new
@@ -18,40 +18,47 @@
def default_scopes(*args)
defaults[:scopes] = args.first.is_a?(Proc) ? args.first : args
end
end
- included do
- private
+ # Returns the default scopes in a hash of hashes with the key containing the scope name
+ # and value containing an array of scope arguments.
+ #
+ # For example:-
+ # {rock: [], electronic: [], released_in_year: ["1980"]}
+ def default_scopes
+ get_scopes
+ end
- def scopes(scopes)
- get_scopes(scopes).each { |name, args| @target = @target.send(name, *args) }
- @target
- end
+ private
- # Accepts a symbol or string scope name, hash containing scope name and argument,
- # lambda/proc to execute to return scope(s) or an array of those types
- # and when the argument is blank it uses the class default scopes.
- # Converts the scopes to a hash of hashes with the key containing the scope name
- # and value containing an array of scope arguments.
- def get_scopes(scopes = nil)
- scopes ||= defaults[:scopes]
+ def scopes(scopes)
+ get_scopes(scopes).each { |name, args| @target = @target.send(name, *args) }
+ @target
+ end
- Array.wrap(scopes).map do |scope|
- case scope
- when Symbol, String
- {scope => []}
- when Hash
- # ensure values are an array so they can be passed to the scope using splat operator
- scope.transform_values! { |v| Array.wrap(v) }
- when Proc
- # if the class default contains a lambda/proc that returns nil
- # don't call get_scopes as we don't want to end up in an infinite loop
- p_scopes = instance_exec(&scope)
- get_scopes(p_scopes) if p_scopes.present?
- end
- end.compact.reduce({}, :merge)
- end
+ # Accepts a symbol or string scope name, hash containing scope name and argument,
+ # lambda/proc to execute to return scope(s) or an array of those types
+ # and when the argument is blank it uses the class default scopes.
+ # Converts the scopes to a hash of hashes with the key containing the scope name
+ # and value containing an array of scope arguments.
+ def get_scopes(scopes = nil)
+ scopes ||= defaults[:scopes]
+
+ Array.wrap(scopes).map do |scope|
+ case scope
+ when Symbol, String
+ {scope => []}
+ when Hash
+ # ensure values are an array so they can be passed to the scope using splat operator
+ scope.transform_values! { |v| Array.wrap(v) }
+ when Proc
+ # if the class default contains a lambda/proc that returns nil
+ # don't call get_scopes as we don't want to end up in an infinite loop
+ p_scopes = instance_exec(&scope)
+ get_scopes(p_scopes) if p_scopes.present?
+ end
+ end.compact.reduce({}, :merge)
end
end
end
end
end