Skip to content

Commit

Permalink
Extract default behavior into a separate module
Browse files Browse the repository at this point in the history
Ditch 1.9 support in the process
  • Loading branch information
rwz committed Aug 29, 2014
1 parent eea0613 commit 94ff86d
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 63 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
rvm:
- 1.9
- 2.0
- 2.1
2 changes: 1 addition & 1 deletion adequate_exposure.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(/\Aspec\//)
spec.require_path = "lib"

spec.required_ruby_version = ">= 1.9.3"
spec.required_ruby_version = "~> 2.0"

spec.add_dependency "railties", "~> 4.0"
spec.add_dependency "activesupport", "~> 4.0"
Expand Down
1 change: 1 addition & 0 deletions lib/adequate_exposure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module AdequateExposure
autoload :Exposure, "adequate_exposure/exposure"
autoload :Attribute, "adequate_exposure/attribute"
autoload :Context, "adequate_exposure/context"
autoload :Behavior, "adequate_exposure/behavior"
autoload :Flow, "adequate_exposure/flow"

ActiveSupport.on_load :action_controller do
Expand Down
56 changes: 56 additions & 0 deletions lib/adequate_exposure/behavior.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module AdequateExposure
module Behavior
def fetch
computed_scope = scope(model)
instance = id ? find(id, computed_scope) : build(build_params, computed_scope)
decorate(instance)
end

def id
params_id_key_candidates.each do |key|
value = params[key]
return value if value.present?
end

nil
end

def scope(model)
model
end

def model
name.to_s.classify.constantize
end

def find(id, scope)
scope.find(id)
end

def build(params, scope)
scope.new(params)
end

def decorate(instance)
instance
end

def build_params
if controller.respond_to?(params_method_name, true) && !get_request?
controller.send(params_method_name)
else
{}
end
end

protected

def params_id_key_candidates
[ "#{model.name.underscore}_id", "#{name}_id", "id" ].uniq
end

def model_param_key
model.name.underscore
end
end
end
4 changes: 1 addition & 3 deletions lib/adequate_exposure/exposure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ def self.expose!(*args, &block)
new(*args, &block).expose!
end

def initialize(controller, name, *args, &block)
def initialize(controller, name, fetch_block=nil, **options, &block)
@controller = controller
options = args.extract_options!
fetch_block = args.pop
@options = options.with_indifferent_access.merge(name: name)

merge_lambda_option :fetch, fetch_block if fetch_block
Expand Down
85 changes: 27 additions & 58 deletions lib/adequate_exposure/flow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,57 +8,16 @@ def initialize(controller, options)
@name = options.fetch(:name)
end

%w[fetch find build build_params scope model id decorate].each do |method_name|
define_method method_name do |*args|
ivar_name = "@#{method_name}"
return instance_variable_get(ivar_name) if instance_variable_defined?(ivar_name)
instance_variable_set(ivar_name, handle_action(method_name, *args))
def method_missing(name, *args, &block)
if respond_to_missing?(name)
handle_flow_method(name, *args, &block)
else
super
end
end

protected

def default_fetch
computed_scope = scope(model)
instance = id ? find(id, computed_scope) : build(build_params, computed_scope)
decorate(instance)
end

def default_id
params_id_key_candidates.each do |key|
value = params[key]
return value if value.present?
end

nil
end

def default_scope(model)
model
end

def default_model
name.to_s.classify.constantize
end

def default_find(id, scope)
scope.find(id)
end

def default_build(params, scope)
scope.new(params)
end

def default_decorate(instance)
instance
end

def default_build_params
if controller.respond_to?(params_method_name, true) && !get_request?
controller.send(params_method_name)
else
{}
end
def respond_to_missing?(method_name, include_private = false)
Behavior.method_defined?(method_name) || super
end

private
Expand All @@ -73,15 +32,17 @@ def params_method_name
options.fetch(:build_params_method){ "#{name}_params" }
end

def handle_action(name, *args)
if options.key?(name)
handle_custom_action(name, *args)
else
send("default_#{name}", *args)
def handle_flow_method(name, *args, &block)
fetch_ivar name do
if options.key?(name)
handle_options_override(name, *args, &block)
else
handle_default_flow_method(name, *args, &block)
end
end
end

def handle_custom_action(name, *args)
def handle_options_override(name, *args)
value = options[name]

if Proc === value
Expand All @@ -92,12 +53,20 @@ def handle_custom_action(name, *args)
end
end

def params_id_key_candidates
[ "#{model.name.underscore}_id", "#{name}_id", "id" ].uniq
def handle_default_flow_method(name, *args, &block)
method = Behavior.instance_method(name)
method.bind(self).call(*args, &block)
end

def model_param_key
model.name.underscore

def fetch_ivar(name)
ivar_name = "@#{name}"

if instance_variable_defined?(ivar_name)
instance_variable_get(ivar_name)
else
instance_variable_set(ivar_name, yield)
end
end
end
end

0 comments on commit 94ff86d

Please sign in to comment.