Skip to content

Commit

Permalink
Support inheritence of columns
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan committed Jun 28, 2012
1 parent faf0fd2 commit d597e55
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
8 changes: 5 additions & 3 deletions lib/datagrid/columns.rb
@@ -1,4 +1,5 @@
require "datagrid/utils"
require "active_support/core_ext/class/attribute"

module Datagrid

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
21 changes: 12 additions & 9 deletions lib/datagrid/core.rb
@@ -1,12 +1,13 @@
require "datagrid/drivers"
require "active_support/core_ext/class/attribute"

module Datagrid
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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -96,18 +98,19 @@ 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

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
Expand Down
13 changes: 13 additions & 0 deletions spec/datagrid/columns_spec.rb
Expand Up @@ -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

0 comments on commit d597e55

Please sign in to comment.