From d597e556f4205e7abfe8016c66083944099ea32c Mon Sep 17 00:00:00 2001 From: Bogdan Gusiev Date: Thu, 28 Jun 2012 23:35:16 +0300 Subject: [PATCH] Support inheritence of columns --- lib/datagrid/columns.rb | 8 +++++--- lib/datagrid/core.rb | 21 ++++++++++++--------- spec/datagrid/columns_spec.rb | 13 +++++++++++++ 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/lib/datagrid/columns.rb b/lib/datagrid/columns.rb index 1fd58302..ecba15d5 100644 --- a/lib/datagrid/columns.rb +++ b/lib/datagrid/columns.rb @@ -1,4 +1,5 @@ require "datagrid/utils" +require "active_support/core_ext/class/attribute" module Datagrid @@ -10,6 +11,8 @@ def self.included(base) base.class_eval do include Datagrid::Core + class_attribute :columns_array + self.columns_array = [] end base.send :include, InstanceMethods @@ -21,7 +24,7 @@ def columns(*args) options = args.extract_options! args.compact! args.map!(&:to_sym) - (@columns ||= []).select do |column| + columns_array.select do |column| (!options[:data] || column.data?) && (args.empty? || args.include?(column.name)) end end @@ -31,8 +34,7 @@ def column(name, options = {}, &block) block ||= lambda do |model| model.send(name) end - @columns ||= [] - @columns << Datagrid::Columns::Column.new(self, name, options, &block) + columns_array << Datagrid::Columns::Column.new(self, name, options, &block) end def column_by_name(name) diff --git a/lib/datagrid/core.rb b/lib/datagrid/core.rb index 95c7f461..d9ee8de7 100644 --- a/lib/datagrid/core.rb +++ b/lib/datagrid/core.rb @@ -1,4 +1,5 @@ require "datagrid/drivers" +require "active_support/core_ext/class/attribute" module Datagrid module Core @@ -6,7 +7,7 @@ module Core def self.included(base) base.extend ClassMethods base.class_eval do - + class_attribute :scope_value end base.send :include, InstanceMethods end # self.included @@ -35,10 +36,10 @@ def datagrid_attributes def scope(&block) if block - @scope = block + self.scope_value = block else check_scope_defined! - @scope.call + scope_value.call end end @@ -47,8 +48,9 @@ def driver end protected - def check_scope_defined!(message = "Scope not defined") - raise(Datagrid::ConfigurationError, message) unless @scope + def check_scope_defined!(message = nil) + message ||= "Scope not defined" + raise(Datagrid::ConfigurationError, message) unless scope_value end end # ClassMethods @@ -96,9 +98,10 @@ def paginate(*args, &block) def scope(&block) if block_given? - @current_scope = block + self.scope_value = block else - @current_scope ? @current_scope.call : self.class.scope + check_scope_defined! + scope_value.call end end @@ -106,8 +109,8 @@ def driver self.class.driver end - def check_scope_defined!(message) - self.class.check_scope_defined!(message) + def check_scope_defined!(message = nil) + self.class.send :check_scope_defined!, message end end # InstanceMethods diff --git a/spec/datagrid/columns_spec.rb b/spec/datagrid/columns_spec.rb index 01ac2d8b..1720a391 100644 --- a/spec/datagrid/columns_spec.rb +++ b/spec/datagrid/columns_spec.rb @@ -60,5 +60,18 @@ report.rows.last.first.should be_false end + it "should inherit columns correctly" do + parent = Class.new do + include Datagrid + scope { Entry } + column(:name) + end + + child = Class.new(parent) do + column(:group_id) + end + child.column_by_name(:name).should_not be_nil + child.column_by_name(:group_id).should_not be_nil + end end