Permalink
Browse files

Abstract Action (for some reason)

Move toward cleaner separation; partly a failed experiment toward
automatically creating the index-handling assumption method
automatically as part of the RESTful default.
  • Loading branch information...
1 parent f40b9b7 commit 18fc275b0d36b8ce5e0c7a017edd7b7ceb1db43b @mattyoho committed Oct 25, 2011
View
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
"lib/basic_assumption/configuration.rb",
"lib/basic_assumption/configuration/active_record.rb",
"lib/basic_assumption/default_assumption.rb",
+ "lib/basic_assumption/default_assumption/action.rb",
"lib/basic_assumption/default_assumption/base.rb",
"lib/basic_assumption/default_assumption/class_resolver.rb",
"lib/basic_assumption/default_assumption/name.rb",
@@ -0,0 +1,56 @@
+module BasicAssumption
+ module DefaultAssumption
+ class Action
+ attr_reader :request, :callbacks
+
+ def initialize(request, &block)
+ @request = request
+ @callbacks = {}
+
+ block.yield self
+ end
+
+ %w(new update find index).each do |name|
+ class_eval <<-MET, __FILE__, __LINE__
+ def #{name}(&block)
+ if block_given?
+ callbacks[:#{name}] = block
+ else
+ (callbacks[:#{name}] || callbacks[:default]).call
+ end
+ end
+ MET
+ end
+
+ def default(&block)
+ callbacks[:default] = block
+ end
+
+ def outcome
+ return index if index?
+ return new if make?
+ return update if update?
+ find
+ end
+
+ private
+
+ def index?
+ action == 'index'
+ end
+
+ def make? #:nodoc:
+ %w(new create).include?(action)
+ end
+
+ def update?
+ %w(update edit).include?(action)
+ end
+
+ def action
+ request.params['action']
+ end
+
+ end
+ end
+end
@@ -1,3 +1,4 @@
+require 'basic_assumption/default_assumption/action'
require 'basic_assumption/default_assumption/class_resolver'
require 'basic_assumption/default_assumption/name'
require 'basic_assumption/default_assumption/owner_builder'
@@ -6,12 +7,14 @@ module BasicAssumption
module DefaultAssumption
# Restful default behavior in the context of Rails
class Rails
- attr_reader :context, :name, :request #:nodoc:
+ attr_reader :action, :context, :name, :request #:nodoc:
def initialize(name=nil, context={}, request=nil) #:nodoc:
- @name = Name.new(context.delete(:as) || name)
@context = context
+ @name = Name.new(context.delete(:as) || name)
@request = request
+
+ @action = initialize_action
end
# Returns a block that will attempt to do the correct thing depending
@@ -95,25 +98,51 @@ def initialize(name=nil, context={}, request=nil) #:nodoc:
# This will create a +sprocket+ method in your actions and view
# that will use the Widget model for its lookup.
def block
- klass = self.class
+ default = self
Proc.new do |name, context|
context[:controller] = self
- klass.new(name, context, request).result
+ default.class.new(name, context, request).result
end
end
def result #:nodoc:
- return list if list?
+ action.outcome
+ end
- if make?
- model_class.new(resource_attributes.merge(owner_attributes))
- elsif lookup?
- lookup_and_maybe_raise
+ protected
+
+ def initialize_action
+ name.plural? ? plural_action : singular_action
+ end
+
+ def singular_action
+ Action.new(request) do |action|
+ action.find do
+ find_and_maybe_raise
+ end
+ action.update do
+ record = find_and_maybe_raise
+ record.attributes = resource_attributes
+ record
+ end
+ action.default do
+ model_class.new(resource_attributes.merge(owner_attributes))
+ end
end
end
- protected
+ def plural_action
+ Action.new(request) do |action|
+ action.default do
+ finder_scope
+ end
+ end
+ end
+
+ def finder_scope
+ model_class.where(owner_attributes)
+ end
def model_class
@model_class ||= ClassResolver.new(name).klass
@@ -123,24 +152,18 @@ def params
@params ||= request ? request.params : {}
end
- def action
- params['action']
- end
-
- def resource_attributes
- params[name.singular] || {}
- end
-
- def lookup_and_maybe_raise
+ def find_and_maybe_raise
begin
- record = model_class.where(owner_attributes).find(lookup_id)
- record.attributes = resource_attributes unless request.get?
- record
+ finder_scope.find(params['id'])
rescue
raise if settings[:raise_error]
end
end
+ def resource_attributes
+ params[name.singular] || {}
+ end
+
def owner_attributes
@owner_attributes ||= if context[:owner]
OwnerBuilder.new(context[:owner], context[:controller]).attributes
@@ -149,26 +172,6 @@ def owner_attributes
end
end
- def list #:nodoc:
- model_class.all
- end
-
- def list? #:nodoc:
- name.plural?
- end
-
- def lookup_id #:nodoc:
- params['id']
- end
-
- def lookup? #:nodoc:
- lookup_id && !list?
- end
-
- def make? #:nodoc:
- %w(new create).include?(action) || !(lookup? || list?)
- end
-
def settings #:nodoc:
@global_settings ||= BasicAssumption::Configuration.settings
@global_settings.merge(context)
@@ -42,22 +42,10 @@ def initialize(hash = {})
%w(create destroy edit index new show update).each do |action|
let(:params) { {'action' => action} }
- context "when there is an id in the params" do
- before { params['id'] = 123 }
- context "when action is #{action}" do
- it "finds all the records of the model class" do
- Model.should_receive(:all)
- default.block.call(name, {})
- end
- end
- end
-
- context "when there is not an id in the params" do
- context "when action is #{action}" do
- it "finds all the records of the model class" do
- Model.should_receive(:all)
- default.block.call(name, {})
- end
+ context "when action is #{action}" do
+ it "finds all the records of the model class" do
+ Model.should_receive(:where)
+ default.block.call(name, {})
end
end
end
@@ -66,21 +54,14 @@ def initialize(hash = {})
context "when the name given to assume is singular" do
let(:name) { :model }
+
context "in the index action" do
let(:params) { { 'action' => 'index' } }
- context "and there is an id in params" do
- before { params['id'] = 1 }
- it "attempts to find a model instance based off the given name" do
- relation.should_receive(:find).with(1).and_return(name)
- default.block.call(name, {}).should eql(name)
- end
- end
- context "and there is no id in params" do
- before { params['model'] = {:initializer => 'value'} }
- it "creates a new model instance and passes in appropriate params" do
- Model.should_receive(:new).with({:initializer => 'value'}).and_return(name)
- default.block.call(name, {}).should eql(name)
- end
+
+ before { params['model'] = {:initializer => 'value'} }
+ it "creates a new model instance and passes in appropriate params" do
+ Model.should_receive(:new).with({:initializer => 'value'}).and_return(name)
+ default.block.call(name, {}).should eql(name)
end
end
@@ -94,22 +75,34 @@ def initialize(hash = {})
end
context "in the edit action" do
- let(:params) { { 'id' => 42, 'action' => 'edit' } }
+ let(:params) { { 'id' => 42, 'action' => 'edit' } }
+ let(:model) { stub("model", :attributes= => {}) }
+
+ before { relation.should_receive(:find).with(42).and_return(model) }
it "attempts to find a model instance based off the given name" do
- relation.should_receive(:find).with(42).and_return(name)
- default.block.call(name, {}).should eql(:model)
+ default.block.call(name, {}).should eql(model)
+ end
+
+ it "sets the updated attributes" do
+ model.should_receive(:attributes=).once
+ default.block.call(name, {})
end
end
context "in the update action" do
- let(:params) do
- { 'id' => 42 }
- end
+ let(:params) { {'id' => 42, 'action' => 'update'} }
+ let(:model) { stub("model", :attributes= => {}) }
+
+ before { relation.should_receive(:find).with(42).and_return(model) }
it "attempts to find a model instance based off the given name" do
- relation.should_receive(:find).with(42).and_return(name)
- default.block.call(name, {}).should eql(:model)
+ default.block.call(name, {}).should eql(model)
+ end
+
+ it "sets the updated attributes" do
+ model.should_receive(:attributes=).once
+ default.block.call(name, {})
end
end
@@ -207,54 +200,4 @@ def owner
end
end
- context "#make?" do
- subject { default.send(:make?) }
- before { default.stub(:list? => false, :lookup? => false) }
- context "when the action is not new or create" do
- context "when #list? is true" do
- before { default.stub(:list? => true) }
- context "when #lookup? is true" do
- before { default.stub(:lookup? => true) }
- it { should be_false }
- end
- context "when #lookup? is false" do
- it { should be_false }
- end
- end
- context "when #list? is false" do
- context "when #lookup? is true" do
- before { default.stub(:lookup? => true) }
- it { should be_false }
- end
- context "when #lookup? is false" do
- it { should be_true }
- end
- end
- end
-
- %w(new create).each do |action|
- context "when the action is #{action}" do
- before { default.stub(:action => action) }
- context "when #list? is true" do
- before { default.stub(:list? => true) }
- context "when #lookup? is true" do
- before { default.stub(:lookup? => true) }
- it { should be_true }
- end
- context "when #lookup? is false" do
- it { should be_true }
- end
- end
- context "when #list? is false" do
- context "when #lookup? is true" do
- before { default.stub(:lookup? => true) }
- it { should be_true }
- end
- context "when #lookup? is false" do
- it { should be_true }
- end
- end
- end
- end
- end
end

0 comments on commit 18fc275

Please sign in to comment.