Module: RailsTables::ModelAdditions::ClassMethods

Defined in:
lib/rails-tables/model_additions.rb

Instance Method Summary collapse

Instance Method Details

#has_datatable(*args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rails-tables/model_additions.rb', line 10

def has_datatable(*args)
  arguments = { }
  if args.present?
    if args.first.is_a? Hash
      arguments = args.pop
    else
      arguments[:name] = args.first if args.try(:first)
      arguments[:klass] = args.second if args.try(:second)
    end
  end
  name = arguments.fetch(:name, 'datatable').to_s
  klass = arguments.fetch(:klass, "#{self.name.pluralize.underscore}_datatable").to_s.camelize
  klass = klass.constantize

  cattr_accessor :datatables unless self.respond_to? :datatables
  self.datatables ||= {}

  # if self.has_datatable? name
  #   raise RailsTables::NameError,
  #     "#{self.name} already has a datatable with the name '#{name}'. "\
  #     "Please supply a :name parameter to has_datatable other than: #{self.datatables.keys.join(', ')}"
  # else
    self.datatables[name] = klass
    cattr_writer name
    self.send("#{name}=", klass.new(name, self))
    self.class.instance_eval do
      define_method name do
        self.class_variable_get("@@#{name}").set_root(self)
      end
    end
  # end
end

#has_datatable?(table_name = :datatable) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/rails-tables/model_additions.rb', line 43

def has_datatable?(table_name=:datatable)
  self.respond_to? :datatables and self.datatables.keys.include? table_name.to_s
end