Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
HamptonMakes committed Nov 23, 2010
2 parents 1e0d20c + d55f607 commit 5476f01
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 24 deletions.
11 changes: 6 additions & 5 deletions lib/resourceful/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Builder
# additions to the controller.
def initialize(kontroller)
@controller = kontroller
@inherited = !kontroller.read_inheritable_attribute(:resourceful_responses).blank?
@inherited = !kontroller.resourceful_responses.blank?
@action_module = Resourceful::Default::Actions.dup
@ok_actions = []
@callbacks = {:before => {}, :after => {}}
Expand All @@ -40,6 +40,7 @@ def apply
apply_publish

kontroller = @controller

Resourceful::ACTIONS.each do |action_named|
# See if this is a method listed by #actions
unless @ok_actions.include? action_named
Expand All @@ -52,11 +53,11 @@ def apply
kontroller.hidden_actions.reject! &@ok_actions.method(:include?)
kontroller.send :include, @action_module

kontroller.read_inheritable_attribute(:resourceful_callbacks).merge! @callbacks
kontroller.read_inheritable_attribute(:resourceful_responses).merge! @responses
kontroller.write_inheritable_attribute(:made_resourceful, true)
kontroller.resourceful_callbacks.merge! @callbacks
kontroller.resourceful_responses.merge! @responses
kontroller.made_resourceful = true

kontroller.write_inheritable_attribute(:parents, @parents)
kontroller.parents = @parents
kontroller.before_filter :load_object, :only => (@ok_actions & SINGULAR_PRELOADED_ACTIONS) + @custom_member_actions
kontroller.before_filter :load_objects, :only => (@ok_actions & PLURAL_ACTIONS) + @custom_collection_actions
kontroller.before_filter :load_parent_object, :only => @ok_actions + @custom_member_actions + @custom_collection_actions
Expand Down
2 changes: 1 addition & 1 deletion lib/resourceful/default/accessors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def object_parameters
#
# Note that the parents must be declared via Builder#belongs_to.
def parent_names
self.class.read_inheritable_attribute :parents
self.class.parents
end

# Returns true if an appropriate parent id parameter has been supplied.
Expand Down
4 changes: 2 additions & 2 deletions lib/resourceful/default/callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def after(event)
# Note that these aren't called directly,
# but instead passed along to Rails' respond_to method.
def response_for(event)
if responses = self.class.read_inheritable_attribute(:resourceful_responses)[event.to_sym]
if responses = self.class.resourceful_responses[event.to_sym]
respond_to do |format|
responses.each do |key, value|
format.send(key, &scope(value))
Expand All @@ -43,7 +43,7 @@ def scope(block)
private

def resourceful_fire(type, name)
callbacks = self.class.read_inheritable_attribute(:resourceful_callbacks)[type][name] || []
callbacks = self.class.resourceful_callbacks[type][name] || []
callbacks.each { |callback| scope(callback).call }
end
end
Expand Down
15 changes: 10 additions & 5 deletions lib/resourceful/maker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ module Maker
# Called automatically on ActionController::Base.
# Initializes various inheritable attributes.
def self.extended(base)
base.write_inheritable_attribute :resourceful_callbacks, {}
base.write_inheritable_attribute :resourceful_responses, {}
base.write_inheritable_attribute :parents, []
base.write_inheritable_attribute :made_resourceful, false
base.class_attribute :resourceful_callbacks
base.class_attribute :resourceful_responses
base.class_attribute :parents
base.class_attribute :made_resourceful

base.resourceful_callbacks = {}
base.resourceful_responses = {}
base.parents = []
base.made_resourceful = false
end

# :call-seq:
Expand Down Expand Up @@ -66,7 +71,7 @@ def make_resourceful(options = {}, &block)
# Returns whether or not make_resourceful has been called
# on this controller or any controllers it inherits from.
def made_resourceful?
read_inheritable_attribute(:made_resourceful)
self.class.made_resourceful
end

private
Expand Down
4 changes: 2 additions & 2 deletions spec/accessors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@
mock_controller Resourceful::Default::Accessors
@parents = %w{post comment}
@models = @parents.map(&:camelize).map(&method(:stub_const))
@kontroller.write_inheritable_attribute(:parents, @parents)
@kontroller.parents = @parents
@controller.stubs(:singular?).returns(false)
@controller.stubs(:instance_variable_name).returns('lines')

Expand Down Expand Up @@ -312,7 +312,7 @@
mock_controller Resourceful::Default::Accessors
@parents = %w{post comment}
@models = @parents.map(&:camelize).map(&method(:stub_const))
@kontroller.write_inheritable_attribute(:parents, @parents)
@kontroller.parents = @parents
@controller.stubs(:params).returns({})
@controller.stubs(:controller_name).returns('line')
stub_const('Line')
Expand Down
2 changes: 1 addition & 1 deletion spec/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

it "should set the controller as made_resourceful" do
@builder.apply
@kontroller.read_inheritable_attribute(:made_resourceful).should be_true
@kontroller.made_resourceful.should be_true
end

it "should set load_parent_object as a before_filter for no actions" do
Expand Down
10 changes: 5 additions & 5 deletions spec/maker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
before(:each) { mock_kontroller }

it "should create an empty, inheritable callbacks hash" do
@kontroller.read_inheritable_attribute(:resourceful_callbacks).should == {}
@kontroller.resourceful_callbacks.should == {}
end

it "should create an empty, inheritable responses hash" do
@kontroller.read_inheritable_attribute(:resourceful_responses).should == {}
@kontroller.resourceful_responses.should == {}
end

it "should create an empty, inheritable parents array" do
@kontroller.read_inheritable_attribute(:parents).should == []
@kontroller.parents.should == []
end

it "should create a made_resourceful variable set to false" do
@kontroller.read_inheritable_attribute(:made_resourceful).should be_false
@kontroller.made_resourceful.should be_false
end

it "should create a made_resourceful? method on the controller that returns the variable" do
@kontroller.should_not be_made_resourceful
@kontroller.write_inheritable_attribute(:made_resourceful, true)
@kontroller.made_resourceful = true
@kontroller.should be_made_resourceful
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ class << @builder
end

def responses
@kontroller.read_inheritable_attribute(:resourceful_responses)
@kontroller.resourceful_responses
end

def callbacks
@kontroller.read_inheritable_attribute(:resourceful_callbacks)
@kontroller.resourceful_callbacks
end

def parents
@kontroller.read_inheritable_attribute(:parents)
@kontroller.parents
end

# Evaluates the made_resourceful block of mod (a module)
Expand Down

0 comments on commit 5476f01

Please sign in to comment.