Permalink
Browse files

Add action jury for availability.

  • Loading branch information...
1 parent 5924bf4 commit 1155620c35e1bffbd81701e0bfb770abd5991f70 @bobbytables committed Dec 22, 2012
@@ -4,6 +4,7 @@ module Actions
autoload :Action, "table_cloth/extensions/actions/action"
autoload :Column, "table_cloth/extensions/actions/column"
autoload :ActionCollection, "table_cloth/extensions/actions/action_collection"
+ autoload :Jury, "table_cloth/extensions/actions/jury"
extend ActiveSupport::Concern
module ClassMethods
@@ -5,5 +5,9 @@ class Action
def initialize(options={})
@options = options
end
+
+ def jury
+ @jury ||= Jury.new(self)
+ end
end
end
@@ -0,0 +1,36 @@
+module TableCloth::Extensions::Actions
+
+ class Jury
+ attr_reader :action
+
+ def initialize(action)
+ @action = action
+ end
+
+ def available?(object)
+ case action_if
+ when Proc
+ return !!action_if.call(object)
+ when Symbol
+ return !!object.send(action_if)
+ end
+
+ case action_unless
+ when Proc
+ return !action_unless.call(object)
+ when Symbol
+ return !object.send(action_unless)
+ end
+ end
+
+ private
+
+ def action_if
+ action.options[:if]
+ end
+
+ def action_unless
+ action.options[:unless]
+ end
+ end
+end
@@ -0,0 +1,6 @@
+FactoryGirl.define do
+ factory :action, class: "TableCloth::Extensions::Actions::Action" do
+ options({})
+ initialize_with { new(options) }
+ end
+end
@@ -2,8 +2,13 @@
describe TableCloth::Extensions::Actions::Action do
let(:action_hash) { Hash.new(block: Proc.new{}) }
+ subject { described_class.new(action_hash) }
+
it "initializes with a hash" do
- action = described_class.new(action_hash)
- expect(action.options).to eq(action_hash)
+ expect(subject.options).to eq(action_hash)
+ end
+
+ it "defines a delegator to a jury" do
+ expect(subject.jury).to be_kind_of TableCloth::Extensions::Actions::Jury
end
end
@@ -0,0 +1,67 @@
+require "spec_helper"
+
+describe TableCloth::Extensions::Actions::Jury do
+ let(:action) { FactoryGirl.build(:action, options: action_options) }
+ subject { described_class.new(action) }
+ let(:model) { double("model") }
+
+ context "Proc" do
+ context "if .available?" do
+ let(:action_options) { {if: Proc.new{|o| o.state == "valid" }} }
+
+ it "returns true for valid models" do
+ model.stub state: "valid"
+ expect(subject).to be_available(model)
+ end
+
+ it "returns false for invalid models" do
+ model.stub state: "invalid"
+ expect(subject).not_to be_available(model)
+ end
+ end
+
+ context "unless .available?" do
+ let(:action_options) { {unless: Proc.new{|o| o.state == "invalid" }} }
+
+ it "returns true for valid models" do
+ model.stub state: "valid"
+ expect(subject).to be_available(model)
+ end
+
+ it "returns false for invalid models" do
+ model.stub state: "invalid"
+ expect(subject).not_to be_available(model)
+ end
+ end
+ end
+
+ context "Symbol" do
+ context "if .available?" do
+ let(:action_options) { {if: :valid?} }
+
+ it "returns true for valid?" do
+ model.stub valid?: true
+ expect(subject).to be_available(model)
+ end
+
+ it "returns true for valid?" do
+ model.stub valid?: false
+ expect(subject).not_to be_available(model)
+ end
+ end
+
+ context "unless .available?" do
+ let(:action_options) { {unless: :valid?} }
+
+ it "returns true for valid models" do
+ model.stub valid?: false
+ expect(subject).to be_available(model)
+ end
+
+ it "returns false for invalid models" do
+ model.stub valid?: true
+ expect(subject).not_to be_available(model)
+ end
+ end
+ end
+end

0 comments on commit 1155620

Please sign in to comment.