Permalink
Browse files

Merge pull request #91 from iwiznia/hiding_columns

Hiding columns using a condition
  • Loading branch information...
2 parents 3f14535 + ae63950 commit ac05f410214d17593c48065cd3e5b7bba05a4ad3 @bogdan committed Mar 6, 2014
Showing with 48 additions and 18 deletions.
  1. +16 −14 lib/datagrid/columns.rb
  2. +18 −4 lib/datagrid/columns/column.rb
  3. +14 −0 spec/datagrid/columns_spec.rb
View
@@ -88,29 +88,31 @@ def columns(*args)
end
# Defines new datagrid column
- #
+ #
# Arguments:
#
# * <tt>name</tt> - column name
# * <tt>options</tt> - hash of options
# * <tt>block</tt> - proc to calculate a column value
#
# Available options:
- #
+ #
# * <tt>:html</tt> - determines if current column should be present in html table and how is it formatted
- # * <tt>:order</tt> - determines if this column could be sortable and how.
- # The value of order is explicitly passed to ORM ordering method.
+ # * <tt>:order</tt> - determines if this column could be sortable and how.
+ # The value of order is explicitly passed to ORM ordering method.
# Ex: <tt>"created_at, id"</tt> for ActiveRecord, <tt>[:created_at, :id]</tt> for Mongoid
- # * <tt>:order_desc</tt> - determines a descending order for given column
+ # * <tt>:order_desc</tt> - determines a descending order for given column
# (only in case when <tt>:order</tt> can not be easily reversed by ORM)
- # * <tt>:order_by_value</tt> - used in case it is easier to perform ordering at ruby level not on database level.
- # Warning: using ruby to order large datasets is very unrecommended.
+ # * <tt>:order_by_value</tt> - used in case it is easier to perform ordering at ruby level not on database level.
+ # Warning: using ruby to order large datasets is very unrecommended.
# If set to true - datagrid will use column value to order by this column
# If block is given - datagrid will use value returned from block
# * <tt>:mandatory</tt> - if true, column will never be hidden with #column_names selection
# * <tt>:url</tt> - a proc with one argument, pass this option to easily convert the value into an URL
# * <tt>:before</tt> - determines the position of this column, by adding it before the column passed here
# * <tt>:after</tt> - determines the position of this column, by adding it after the column passed here
+ # * <tt>:if</tt> - the column is shown if the reult of calling this argument is true
+ # * <tt>:unless</tt> - the column is shown unless the reult of calling this argument is true
#
# See: https://github.com/bogdan/datagrid/wiki/Columns for examples
def column(name, options = {}, &block)
@@ -148,7 +150,7 @@ def format(value, &block)
end
end
else
- # Ruby Object#format exists.
+ # Ruby Object#format exists.
# We don't want to change the behaviour and overwrite it.
super
end
@@ -212,7 +214,7 @@ def data(*column_names)
self.rows(*column_names).unshift(self.header(*column_names))
end
- # Return Array of Hashes where keys are column names and values are column values
+ # Return Array of Hashes where keys are column names and values are column values
# for each row in filtered datagrid relation.
#
# Example:
@@ -267,13 +269,13 @@ def to_csv(*column_names)
# Returns all columns selected in grid instance
#
# Examples:
- #
+ #
# MyGrid.new.columns # => all defined columns
# grid = MyGrid.new(:column_names => [:id, :name])
# grid.columns # => id and name columns
# grid.columns(:id, :category) # => id and category column
def columns(*args)
- self.class.columns(*args)
+ self.class.columns(*args).select {|column| column.enabled?(self)}
end
# Returns all columns that can be represented in plain data(non-html) way
@@ -301,15 +303,15 @@ def column_by_name(name)
# Gives ability to have a different formatting for CSV and HTML column value.
#
# Example:
- #
+ #
# column(:name) do |model|
# format(model.name) do |value|
# content_tag(:strong, value)
# end
# end
#
# column(:company) do |model|
- # format(model.company.name) do
+ # format(model.company.name) do
# render :partial => "company_with_logo", :locals => {:company => model.company }
# end
# end
@@ -326,7 +328,7 @@ def format(value, &block)
# Allows to access column values
#
# Example:
- #
+ #
# class MyGrid
# scope { User }
# column(:id)
@@ -40,6 +40,8 @@ def initialize(grid_class, name, options = {}, &block)
self.html_block = options[:html]
end
end
+ options[:if] = convert_option_to_proc(options[:if])
+ options[:unless] = convert_option_to_proc(options[:unless])
end
def data_value(model, grid)
@@ -54,7 +56,7 @@ def label
end
def header
- self.options[:header] ||
+ self.options[:header] ||
I18n.translate(self.name, :scope => "datagrid.#{self.grid_class.param_name}.columns", :default => self.name.to_s.humanize )
end
@@ -84,21 +86,25 @@ def order_by_value?
def order_desc
return nil unless order
- self.options[:order_desc]
+ self.options[:order_desc]
end
def html?
options[:html] != false
end
-
+
def data?
self.data_block != nil
end
-
+
def mandatory?
!! options[:mandatory]
end
+ def enabled?(grid)
+ (!options[:if] || (options[:if] && options[:if].call(grid))) && !options[:unless] || (options[:unless] && !options[:unless].call(grid))
+ end
+
def inspect
"#<Datagird::Columns::Column #{grid_class}##{name} #{options.inspect}>"
end
@@ -144,4 +150,12 @@ def generic_value(model, grid)
end
end
+ private
+ def convert_option_to_proc(option)
+ if option.is_a?(Proc)
+ option
+ elsif option
+ proc {|object| object.send(option.to_sym) }
+ end
+ end
end
@@ -82,6 +82,20 @@
it "should support csv export options" do
subject.to_csv(:col_sep => ";").should == "Shipping date;Group;Name;Access level;Pet\n#{date};Pop;Star;admin;ROTTWEILER\n"
end
+
+ it "should support hidding columns through if and unless" do
+ report = test_report do
+ scope {Entry}
+ column(:id, :if => :show?)
+ column(:name, :unless => proc {|grid| !grid.show? })
+
+ def show?
+ false
+ end
+ end
+ report.columns(:id).should == []
+ report.columns(:name).should == []
+ end
end
it "should support columns with model and report arguments" do

0 comments on commit ac05f41

Please sign in to comment.