Class: ContentsCore::Item
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.permitted_attributes ⇒ Object
100
101
102
|
# File 'app/models/contents_core/item.rb', line 100
def self.permitted_attributes
[ :data_boolean, :data_datetime, :data_file, :data_float, :data_hash, :data_integer, :data_string, :data_text ]
end
|
.type_name ⇒ Object
104
105
106
|
# File 'app/models/contents_core/item.rb', line 104
def self.type_name
''
end
|
.types ⇒ Object
108
109
110
|
# File 'app/models/contents_core/item.rb', line 108
def self.types
@@types ||= ContentsCore.config[:items].keys.map( &:to_sym )
end
|
Instance Method Details
#as_json(options = nil) ⇒ Object
24
25
26
|
# File 'app/models/contents_core/item.rb', line 24
def as_json( options = nil )
super( {only: [:id, :name, :type], methods: [:data]}.merge(options || {}) )
end
|
#attr_id ⇒ Object
28
29
30
|
# File 'app/models/contents_core/item.rb', line 28
def attr_id
"#{self.class_name}-#{self.id}"
end
|
#class_name ⇒ Object
32
33
34
|
# File 'app/models/contents_core/item.rb', line 32
def class_name
self.class.to_s.split( '::' ).last
end
|
#config ⇒ Object
36
37
38
|
# File 'app/models/contents_core/item.rb', line 36
def config
@config ||= ( ContentsCore.config[:items] && ContentsCore.config[:items][self.class_name.underscore.to_sym] ? ContentsCore.config[:items][self.class_name.underscore.to_sym] : {} ).merge( self.block && self.block.config[:options] && self.name && self.block.config[:options][self.name.to_sym] ? self.block.config[:options][self.name.to_sym] : {} )
end
|
#data_type ⇒ Object
40
41
42
|
# File 'app/models/contents_core/item.rb', line 40
def data_type
@data_type ||= ( config[:data_type] || :string ).to_sym
end
|
#editable ⇒ Object
44
45
46
|
# File 'app/models/contents_core/item.rb', line 44
def editable
ContentsCore.editing ? " data-ec-item=\"#{self.id}\" data-ec-input=\"#{self.opt_input}\" data-ec-type=\"#{self.class_name}\"".html_safe : ''
end
|
#init ⇒ Object
placeholder method (for override)
48
49
|
# File 'app/models/contents_core/item.rb', line 48
def init
end
|
#on_after_initialize ⇒ Object
— methods ————————————————————-
19
20
21
22
|
# File 'app/models/contents_core/item.rb', line 19
def on_after_initialize
self.data = config[:default] if config[:default] && !self.data
self.init
end
|
#on_before_create ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'app/models/contents_core/item.rb', line 51
def on_before_create
names = ( self.block.items - [self] ).pluck :name
if self.name.blank? || names.include?( self.name )
n = self.name.blank? ? self.class.type_name : self.name
i = 0
while( ( i += 1 ) < 1000 )
unless names.include? "#{n}-#{i}"
self.name = "#{n}-#{i}"
break
end
end
end
end
|
68
69
70
71
72
73
74
75
76
|
# File 'app/models/contents_core/item.rb', line 68
def opt_input
if self.block.config[self.name] && self.block.config[self.name]['input']
self.block.config[self.name]['input'].to_s
elsif config[:input]
config[:input].to_s
else
''
end
end
|
#process_data(args = nil) ⇒ Object
78
79
80
|
# File 'app/models/contents_core/item.rb', line 78
def process_data( args = nil )
config[:process_data].call( self.data, args ) if config[:process_data]
end
|
#set(value) ⇒ Object
82
83
84
85
|
# File 'app/models/contents_core/item.rb', line 82
def set( value )
self.data = value
self
end
|
#to_s ⇒ Object
87
88
89
|
# File 'app/models/contents_core/item.rb', line 87
def to_s
self.data
end
|
#update_data(value) ⇒ Object
91
92
93
94
|
# File 'app/models/contents_core/item.rb', line 91
def update_data( value )
self.data = value
self.save
end
|
#validate_item ⇒ Object
96
97
98
|
# File 'app/models/contents_core/item.rb', line 96
def validate_item
config[:validate].call( self ) if config[:validate]
end
|