Class: PushType::RelationField
Instance Attribute Summary
Attributes inherited from FieldType
#model, #name
Instance Method Summary
collapse
Methods inherited from FieldType
#css_class, #field_options, #form_helper, #json_value, #kind, #multiple?, on_class, on_instance, options, #primitive, #template, #value
Constructor Details
Returns a new instance of RelationField.
6
7
8
9
|
# File 'app/fields/push_type/relation_field.rb', line 6
def initialize(*args)
super
raise ArgumentError, "Relation field names must end with suffix `_id` or `_ids`." unless relation_name
end
|
Instance Method Details
23
24
25
26
27
28
29
|
# File 'app/fields/push_type/relation_field.rb', line 23
def choices
if relation_items.is_a? Hash
flatten_tree(relation_items)
else
relation_items.map { |item| item_hash(item) }
end
end
|
#html_options ⇒ Object
19
20
21
|
# File 'app/fields/push_type/relation_field.rb', line 19
def html_options
super.merge(multiple: multiple?)
end
|
#json_primitive ⇒ Object
11
12
13
|
# File 'app/fields/push_type/relation_field.rb', line 11
def json_primitive
multiple? ? :array : super
end
|
15
16
17
|
# File 'app/fields/push_type/relation_field.rb', line 15
def label
super || relation_name.humanize
end
|
#relation_class ⇒ Object
35
36
37
|
# File 'app/fields/push_type/relation_field.rb', line 35
def relation_class
@relation_class ||= (@opts[:to] || relation_name.singularize).to_s.classify.constantize
end
|
#relation_items ⇒ Object
39
40
41
42
43
44
45
46
47
|
# File 'app/fields/push_type/relation_field.rb', line 39
def relation_items
@relation_items ||= begin
if @opts[:scope].respond_to? :call
relation_class.instance_exec(&@opts[:scope])
else
relation_class.all
end
end
end
|
#relation_name ⇒ Object
31
32
33
|
# File 'app/fields/push_type/relation_field.rb', line 31
def relation_name
@relation_name ||= (rel = name.to_s.gsub!(/_ids?$/, '')) && (rel && multiple? ? rel.pluralize : rel)
end
|