Skip to content

Commit

Permalink
Converted to a gem.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmorrison committed Oct 26, 2010
1 parent 1b72c5e commit 44bd9a3
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 53 deletions.
2 changes: 2 additions & 0 deletions .bundle/config
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,2 @@
---
BUNDLE_DISABLE_SHARED_GEMS: "1"
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,2 @@
source :gemcutter
gemspec
49 changes: 49 additions & 0 deletions Gemfile.lock
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,49 @@
PATH
remote: .
specs:
with_action (1.0.0)
actionpack
activesupport

GEM
remote: http://rubygems.org/
specs:
abstract (1.0.0)
actionpack (3.0.1)
activemodel (= 3.0.1)
activesupport (= 3.0.1)
builder (~> 2.1.2)
erubis (~> 2.6.6)
i18n (~> 0.4.1)
rack (~> 1.2.1)
rack-mount (~> 0.6.12)
rack-test (~> 0.5.4)
tzinfo (~> 0.3.23)
activemodel (3.0.1)
activesupport (= 3.0.1)
builder (~> 2.1.2)
i18n (~> 0.4.1)
activesupport (3.0.1)
builder (2.1.2)
erubis (2.6.6)
abstract (>= 1.0.0)
i18n (0.4.1)
mocha (0.9.9)
rake
rack (1.2.1)
rack-mount (0.6.13)
rack (>= 1.0.0)
rack-test (0.5.6)
rack (>= 1.0)
rake (0.8.7)
tzinfo (0.3.23)

PLATFORMS
ruby

DEPENDENCIES
actionpack
activesupport
bundler (>= 1.0.0)
mocha
with_action!
4 changes: 3 additions & 1 deletion Rakefile
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'rake' require 'bundler'
Bundler::GemHelper.install_tasks

require 'rake/testtask' require 'rake/testtask'
require 'rake/rdoctask' require 'rake/rdoctask'


Expand Down
5 changes: 0 additions & 5 deletions init.rb

This file was deleted.

47 changes: 6 additions & 41 deletions lib/with_action.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,42 +1,7 @@
module CollectiveIdea require 'active_support'
module WithAction require 'action_controller'
def with_action(*actions) require 'with_action/with_action'
responder = ActionResponder.new(self)
yield responder if block_given? ActionController::Base.class_eval do
actions.each {|a| responder.send(a) } include WithAction
responder.respond
end

class ActionResponder
def initialize(controller)
@controller = controller
@order, @responses = [], {}
end

def respond
action = @order.detect do |a|
!@controller.params[a].blank? || !@controller.params["#{a}.x"].blank?
end
action ||= @order.include?(:any) ? :any : @order.first
@responses[action].call if @responses[action]
end

def any(&block)
method_missing(:any) do
# reset
@order, @responses = [], {}
block.call.tap do
respond
end
end
end

def method_missing(action, &block)
@order << action
block ||= lambda { @controller.send(action) }
@responses[action] = block
end
end

end
end end
3 changes: 3 additions & 0 deletions lib/with_action/version.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,3 @@
module WithAction
VERSION = "1.0.0"
end
40 changes: 40 additions & 0 deletions lib/with_action/with_action.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,40 @@
module WithAction
def with_action(*actions)
responder = ActionResponder.new(self)
yield responder if block_given?
actions.each {|a| responder.send(a) }
responder.respond
end

class ActionResponder
def initialize(controller)
@controller = controller
@order, @responses = [], {}
end

def respond
action = @order.detect do |a|
!@controller.params[a].blank? || !@controller.params["#{a}.x"].blank?
end
action ||= @order.include?(:any) ? :any : @order.first
@responses[action].call if @responses[action]
end

def any(&block)
method_missing(:any) do
# reset
@order, @responses = [], {}
block.call.tap do
respond
end
end
end

def method_missing(action, &block)
@order << action
block ||= lambda { @controller.send(action) }
@responses[action] = block
end
end

end
4 changes: 0 additions & 4 deletions tasks/with_action_tasks.rake

This file was deleted.

4 changes: 2 additions & 2 deletions test/with_action_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
require File.dirname(__FILE__) + '/../lib/with_action' require File.dirname(__FILE__) + '/../lib/with_action'


class WithActionTest < Test::Unit::TestCase class WithActionTest < Test::Unit::TestCase
include CollectiveIdea::WithAction include WithAction


def setup def setup
@params = {} @params = {}
@responder = CollectiveIdea::WithAction::ActionResponder.new(self) @responder = WithAction::ActionResponder.new(self)
end end


def params def params
Expand Down
25 changes: 25 additions & 0 deletions with_action.gemspec
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- encoding: utf-8 -*-
require File.expand_path("../lib/with_action/version", __FILE__)

Gem::Specification.new do |s|
s.name = "with_action"
s.version = WithAction::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Brandon Keepers", "Daniel Morrison"]
s.email = ['info@collectiveidea.com']
s.homepage = "http://rubygems.org/gems/with_action"
s.summary = "A respond_to style helper for Rails controllers for doing different actions based on which request parameters are passed."
s.description = 'A respond_to style helper for doing different actions based on which request parameters are passed. Specifically, it is helpful if you want to use multiple form buttons on a page, such as "Save", "Save and Continue Editing", and "Cancel". with_action executes different blocks based on what the presence of request parameters.'

s.required_rubygems_version = ">= 1.3.6"
s.rubyforge_project = "with_action"

s.add_dependency "actionpack"

s.add_development_dependency "bundler", ">= 1.0.0"
s.add_development_dependency "mocha"

s.files = `git ls-files`.split("\n")
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
s.require_path = 'lib'
end

1 comment on commit 44bd9a3

@dhirengupta
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please tell how to install with_action. Thanks

Please sign in to comment.