Skip to content

Commit

Permalink
Make grids inheritable
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan authored and barendt committed Aug 7, 2012
1 parent 2ed9501 commit 570b9b1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
6 changes: 6 additions & 0 deletions lib/datagrid/columns.rb
Expand Up @@ -42,6 +42,12 @@ def column_by_name(name)
col.name.to_sym == name.to_sym
end
end

def inherited(child_class)
super(child_class)
child_class.columns_array = self.columns_array.clone
end

end # ClassMethods

module InstanceMethods
Expand Down
14 changes: 9 additions & 5 deletions lib/datagrid/filters.rb
@@ -1,3 +1,5 @@
require "active_support/core_ext/class/attribute"

module Datagrid
module Filters

Expand Down Expand Up @@ -29,24 +31,21 @@ def self.included(base)

include Datagrid::Core
include Datagrid::Filters::CompositeFilters
class_attribute :filters
self.filters = []

end
base.send :include, InstanceMethods
end # self.included

module ClassMethods

def filters
@filters ||= []
end

def filter_by_name(attribute)
self.filters.find do |filter|
filter.name.to_sym == attribute.to_sym
end
end


def filter(attribute, *args, &block)
options = args.extract_options!
type = args.shift || :default
Expand All @@ -73,6 +72,11 @@ def default_filter(attribute)
end
end

def inherited(child_class)
super(child_class)
child_class.filters = self.filters.clone
end

end # ClassMethods

module InstanceMethods
Expand Down
2 changes: 2 additions & 0 deletions spec/datagrid/columns_spec.rb
Expand Up @@ -70,6 +70,8 @@
child = Class.new(parent) do
column(:group_id)
end
parent.column_by_name(:name).should_not be_nil
parent.column_by_name(:group_id).should be_nil
child.column_by_name(:name).should_not be_nil
child.column_by_name(:group_id).should_not be_nil
end
Expand Down
13 changes: 13 additions & 0 deletions spec/datagrid/filters_spec.rb
Expand Up @@ -55,6 +55,19 @@ class TheReport
TheReport.new(:name => 'hello')
end

it "should support inheritence" do
parent = Class.new do
include Datagrid
scope {Entry}
filter(:name)
end
child = Class.new(parent) do
filter(:group_id)
end
parent.filters.size.should == 1
child.filters.size.should == 2
end

describe "allow_blank and allow_nil options" do

def check_performed(value, result, options)
Expand Down

0 comments on commit 570b9b1

Please sign in to comment.