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 ||= {}
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
|