Permalink
Browse files

Merge pull request #54 from bikeexchange/allow_data_for_html_columns

:data option to provide CSV alternative to :html option
  • Loading branch information...
2 parents 38aabed + 8ce8304 commit 524d40ec89e0aea429de2241f6d7f2d57b874ce0 @bogdan committed Dec 11, 2012
@@ -1,12 +1,19 @@
class Datagrid::Columns::Column
- attr_accessor :grid, :options, :block, :name
+ attr_accessor :grid, :options, :block, :name, :html_block
def initialize(grid, name, options = {}, &block)
self.grid = grid
self.name = name.to_sym
self.options = options
- self.block = block
+ if options[:html] == true
+ self.html_block = block
+ else
+ if options[:html].is_a? Proc
+ self.html_block = options[:html]
+ end
+ self.block = block
+ end
if format
::Datagrid::Utils.warn_once(":format column option is deprecated. Use :url or :html option instead.")
end
@@ -53,11 +60,11 @@ def order_desc
end
def html?
- !! self.options[:html]
+ self.html_block != nil
end
def data?
- !html?
+ self.block != nil
end
end
View
@@ -12,15 +12,24 @@ def initialize(template)
end
def format_value(grid, column, asset)
+
value = if column.html?
- if column.block.arity > 1
- @template.instance_exec(asset, grid, &column.block)
- else
- @template.instance_exec(asset, &column.block)
+ args = []
+ remaining_arity = column.html_block.arity
+
+ if column.data?
+ args << column.value(asset,grid)
+ remaining_arity -= 1
end
+
+ args << asset if remaining_arity > 0
+ args << grid if remaining_arity > 1
+
+ @template.instance_exec(*args, &column.html_block)
else
- column.value(asset, grid)
+ column.value(asset,grid)
end
+
url = column.options[:url] && column.options[:url].call(asset)
if url
@template.link_to(value, url)
@@ -11,17 +11,23 @@
describe "basic methods" do
let!(:entry) { Entry.create!(
- :group => group, :name => "Star", :disabled => false, :confirmed => false, :category => "first"
+ :group => group,
+ :name => "Star",
+ :disabled => false,
+ :confirmed => false,
+ :category => "first",
+ :access_level => 'admin',
+ :pet => 'rottweiler'
) }
it "should have data columns without html columns" do
subject.data_columns.size.should == subject.columns.size - 1
end
it "should build rows of data" do
- subject.rows.should == [["Pop", "Star"]]
+ subject.rows.should == [["Pop", "Star", "admin", "ROTTWEILER"]]
end
it "should generate header" do
- subject.header.should == ["Group", "Name"]
+ subject.header.should == ["Group", "Name", "Access level", "Pet"]
end
it "should generate table data" do
@@ -34,12 +40,14 @@
it "should generate hash for given asset" do
subject.hash_for(entry).should == {
:group => "Pop",
- :name => "Star"
+ :name => "Star",
+ :access_level => 'admin',
+ :pet => 'ROTTWEILER'
}
end
it "should support csv export" do
- subject.to_csv.should == "Group,Name\nPop,Star\n"
+ subject.to_csv.should == "Group,Name,Access level,Pet\nPop,Star,admin,ROTTWEILER\n"
end
it "should support csv export of particular columns" do
@@ -97,7 +97,6 @@
end
describe ".datagrid_rows" do
-
it "should support urls" do
rp = test_report do
scope { Entry }
@@ -107,16 +106,17 @@
"tr td.name a[href=Star]" => "Star"
)
end
+
it "should support conditional urls" do
rp = test_report do
scope { Entry }
column(:name, :url => lambda {|model| false})
end
subject.datagrid_rows(rp, [entry]).should match_css_pattern(
"tr td.name" => "Star"
- )
-
+ )
end
+
it "should add ordering classes to column" do
rp = test_report(:order => :name) do
scope { Entry }
@@ -125,8 +125,8 @@
subject.datagrid_rows(rp, [entry]).should match_css_pattern(
"tr td.name.ordered.asc" => "Star"
)
-
end
+
it "should add ordering classes to column" do
rp = test_report(:order => :name, :descending => true) do
scope { Entry }
@@ -138,7 +138,6 @@
end
it "should render html columns" do
-
rp = test_report do
scope { Entry }
column(:name, :html => true) do |model|
@@ -150,6 +149,28 @@
)
end
+ it "should render argument-based html columns" do
+ rp = test_report do
+ scope { Entry }
+ column(:name, :html => lambda {|data| content_tag :h1, data})
+ end
+ subject.datagrid_rows(rp, [entry]).should match_css_pattern(
+ "tr td.name h1" => "Star"
+ )
+ end
+
+ it "should render argument-based html columns with custom data" do
+ rp = test_report do
+ scope { Entry }
+ column(:name, :html => lambda {|data| content_tag :em, data}) do
+ self.name.upcase
+ end
+ end
+ subject.datagrid_rows(rp, [entry]).should match_css_pattern(
+ "tr td.name em" => "STAR"
+ )
+ end
+
it "should render html columns with double arguments for column" do
rp = test_report do
scope { Entry }
@@ -162,6 +183,58 @@
)
end
+ it "should render argument-based html blocks with double arguments" do
+ rp = test_report do
+ scope { Entry }
+ column(:name, :html => lambda { |data, model|
+ content_tag :h1, "#{data}-#{model.name.downcase}"
+ })
+ end
+ subject.datagrid_rows(rp, [entry]).should match_css_pattern(
+ "tr td.name h1" => "Star-star"
+ )
+ end
+
+ it "should render argument-based html blocks with triple arguments" do
+ rp = test_report do
+ scope { Entry }
+ column(:name, :html => lambda { |data, model, grid|
+ content_tag :h1, "#{data}-#{model.name.downcase}-#{grid.assets.klass}"
+ })
+ end
+ subject.datagrid_rows(rp, [entry]).should match_css_pattern(
+ "tr td.name h1" => "Star-star-Entry"
+ )
+ end
+
+ it "should render argument-based html blocks with double arguments and custom data" do
+ rp = test_report do
+ scope { Entry }
+ column(:name, :html => lambda { |data, model|
+ content_tag :h1, "#{data}-#{model.name}"
+ }) do
+ self.name.upcase
+ end
+ end
+ subject.datagrid_rows(rp, [entry]).should match_css_pattern(
+ "tr td.name h1" => "STAR-Star"
+ )
+ end
+
+ it "should render argument-based html blocks with triple arguments and custom data" do
+ rp = test_report do
+ scope { Entry }
+ column(:name, :html => lambda { |data, model, grid|
+ content_tag :h1, "#{data}-#{model.name}-#{grid.assets.klass}"
+ }) do
+ self.name.upcase
+ end
+ end
+ subject.datagrid_rows(rp, [entry]).should match_css_pattern(
+ "tr td.name h1" => "STAR-Star-Entry"
+ )
+ end
+
it "should support columns option" do
rp = test_report do
scope { Entry }
@@ -14,6 +14,8 @@
t.integer :group_id
t.string :name
t.string :category
+ t.string :access_level
+ t.string :pet
t.boolean :disabled, :null => false, :default => false
t.boolean :confirmed, :null => false, :default => false
t.date :shipping_date
@@ -40,6 +40,12 @@ class SimpleReport
render :partial => "actions", :locals => {:model => model}
end
+ column(:access_level, :html => lambda {|data| content_tag :h1, data})
+
+ column(:pet, :html => lambda {|data| content_tag :em, data}) do
+ self.pet.try(:upcase)
+ end
+
def param_name
:report
end

0 comments on commit 524d40e

Please sign in to comment.