Skip to content

Commit

Permalink
Allow for TabulatrData inheritance
Browse files Browse the repository at this point in the history
One can inherit and expand TabulatrData for a given relation,
e.g. by adding extra columns and/or filters
in addition to those in parent class.
  • Loading branch information
mlt committed Nov 28, 2019
1 parent 336d2a7 commit 8ebd256
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
15 changes: 13 additions & 2 deletions lib/tabulatr/data/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ def execute_batch_actions batch_param, selected_ids
end

def table_columns
self.class.instance_variable_get("@table_columns")
collect :@table_columns
end

def filters
self.class.instance_variable_get('@filters')
collect :@filters
end

def search?
Expand Down Expand Up @@ -153,6 +153,17 @@ def join_required_tables(params)
# @relation = @relation.group("#{@table_name}.#{@base.primary_key}")
end

private

def collect(variable)
self.class.ancestors.inject([]) do |cols, tdc|
if tdc < Tabulatr::Data
tc = tdc.instance_variable_get variable
cols += tc if tc
end
cols
end
end
end

require_relative './dsl'
Expand Down
2 changes: 1 addition & 1 deletion lib/tabulatr/data/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def filter(name, partial: nil, &block)

def target_class(name)
s = name.to_s
@target_class = s.camelize.constantize rescue "There's no class `#{s.camelize}' for `#{self.name}'"
@target_class = s.camelize.safe_constantize
@target_class_name = s.underscore
end

Expand Down
11 changes: 5 additions & 6 deletions lib/tabulatr/renderer/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,11 @@ def get_requested_filters(available_filters, requested_filters)
end.flatten
end

def get_data_class(name = '')
if name.present?
name.constantize.new(@klass)
else
"#{@klass.name}TabulatrData".constantize.new(@klass)
end
def get_data_class(name = nil)
name = "#{@klass.name}TabulatrData" if name.blank?
name = name.constantize if name.is_a?(String)

name.new(@klass)
end

def set_columns_and_filters(data_class, columns, filters, &block)
Expand Down

0 comments on commit 8ebd256

Please sign in to comment.