Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial work on actions.

  • Loading branch information...
commit 5924bf4c8a97b3df446462c07ee94bd3bfef72b2 1 parent db0b655
@bobbytables authored
View
28 lib/table_cloth.rb
@@ -1,19 +1,23 @@
-require 'action_view'
-require 'active_support/core_ext/class'
-require 'table_cloth/version'
-require 'table_cloth/configurable_elements'
-require 'table_cloth/base'
+require "action_view"
+require "active_support/core_ext/class"
+require "table_cloth/version"
+require "table_cloth/configurable_elements"
+require "table_cloth/base"
module TableCloth
- autoload :Configuration, 'table_cloth/configuration'
- autoload :Builder, 'table_cloth/builder'
- autoload :Column, 'table_cloth/column'
- autoload :ColumnJury, 'table_cloth/column_jury'
- autoload :Presenter, 'table_cloth/presenter'
- autoload :ActionViewExtension, 'table_cloth/action_view_extension'
+ autoload :Configuration, "table_cloth/configuration"
+ autoload :Builder, "table_cloth/builder"
+ autoload :Column, "table_cloth/column"
+ autoload :ColumnJury, "table_cloth/column_jury"
+ autoload :Presenter, "table_cloth/presenter"
+ autoload :ActionViewExtension, "table_cloth/action_view_extension"
module Presenters
- autoload :Default, 'table_cloth/presenters/default'
+ autoload :Default, "table_cloth/presenters/default"
+ end
+
+ module Extensions
+ autoload :Actions, "table_cloth/extensions/actions"
end
def config_for(type)
View
24 lib/table_cloth/extensions/actions.rb
@@ -0,0 +1,24 @@
+module TableCloth
+ module Extensions
+ module Actions
+ autoload :Action, "table_cloth/extensions/actions/action"
+ autoload :Column, "table_cloth/extensions/actions/column"
+ autoload :ActionCollection, "table_cloth/extensions/actions/action_collection"
+ extend ActiveSupport::Concern
+
+ module ClassMethods
+ def actions(options={}, &block)
+ action_collection.instance_eval(&block)
+
+ column :actions, options.update(collection: action_collection, using: Column)
+ end
+
+ private
+
+ def action_collection
+ @action_collection ||= ActionCollection.new
+ end
+ end
+ end
+ end
+end
View
9 lib/table_cloth/extensions/actions/action.rb
@@ -0,0 +1,9 @@
+module TableCloth::Extensions::Actions
+ class Action
+ attr_reader :options
+
+ def initialize(options={})
+ @options = options
+ end
+ end
+end
View
11 lib/table_cloth/extensions/actions/action_collection.rb
@@ -0,0 +1,11 @@
+module TableCloth::Extensions::Actions
+ class ActionCollection
+ def actions
+ @actions ||= []
+ end
+
+ def action(&block)
+ actions << Action.new(proc: block)
+ end
+ end
+end
View
5 lib/table_cloth/extensions/actions/column.rb
@@ -0,0 +1,5 @@
+module TableCloth::Extensions::Actions
+ class Column < ::TableCloth::Column
+
+ end
+end
View
24 spec/lib/extensions/actions/action_collection_spec.rb
@@ -0,0 +1,24 @@
+require "spec_helper"
+
+describe TableCloth::Extensions::Actions::ActionCollection do
+ subject { described_class.new }
+
+ context "#action" do
+ it "adds an action" do
+ subject.action { }
+ expect(subject).to have(1).actions
+ end
+ end
+
+ context "#actions" do
+ before(:each) do
+ subject.action { }
+ end
+
+ it "returns a list of actions" do
+ subject.actions.each do |action|
+ expect(action).to be_kind_of TableCloth::Extensions::Actions::Action
+ end
+ end
+ end
+end
View
9 spec/lib/extensions/actions/action_spec.rb
@@ -0,0 +1,9 @@
+require "spec_helper"
+
+describe TableCloth::Extensions::Actions::Action do
+ let(:action_hash) { Hash.new(block: Proc.new{}) }
+ it "initializes with a hash" do
+ action = described_class.new(action_hash)
+ expect(action.options).to eq(action_hash)
+ end
+end
View
42 spec/lib/extensions/actions_spec.rb
@@ -0,0 +1,42 @@
+require "spec_helper"
+
+describe TableCloth::Extensions::Actions do
+ let(:dummy_table) { FactoryGirl.build(:dummy_table) }
+
+ context "inclusion" do
+ it "gives the table class an actions method" do
+ expect { dummy_table.send(:include, described_class) }.to change { dummy_table.respond_to? :actions }.to true
+ end
+
+ context ".actions" do
+ before(:each) { dummy_table.send(:include, described_class) }
+
+ it "yields an ActionCollection block" do
+ block_type = nil
+ dummy_table.actions { block_type = self }
+ expect(block_type).to be_kind_of TableCloth::Extensions::Actions::ActionCollection
+ end
+
+ it "creates an actions column on the table" do
+ dummy_table.actions { }
+ expect(dummy_table.columns).to have_key :actions
+ end
+
+ it "accepts options" do
+ dummy_table.actions(if: :admin?) { }
+ expect(dummy_table.columns[:actions][:options]).to have_key :if
+ end
+
+ it "sets a collection key for the column pointing to the collection object" do
+ dummy_table.actions { }
+ expect(dummy_table.columns[:actions][:options][:collection]).to be_kind_of TableCloth::Extensions::Actions::ActionCollection
+ end
+
+ it "sets the column class to an action column" do
+ dummy_table.actions { }
+ column = dummy_table.columns[:actions]
+ expect(column[:class]).to eq(TableCloth::Extensions::Actions::Column)
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.