Class: ModelBase::ColumnAttribute
- Inherits:
-
Rails::Generators::GeneratedAttribute
- Object
- Rails::Generators::GeneratedAttribute
- ModelBase::ColumnAttribute
show all
- Defined in:
- lib/model_base/column_attribute.rb
Defined Under Namespace
Classes: AbstractSelectRenderer, EnumerizedSelectRenderer, ReferenceSelectRenderer
Constant Summary
collapse
- LOCALIZED_TYPES =
[:time, :datetime].freeze
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(model, name, type, column: nil, reference: nil, index_type: false, title: false, attr_options: {}) ⇒ ColumnAttribute
Returns a new instance of ColumnAttribute.
13
14
15
16
17
18
19
|
# File 'lib/model_base/column_attribute.rb', line 13
def initialize(model, name, type, column: nil, reference: nil, index_type: false, title: false, attr_options: {})
super(name, type, index_type, attr_options)
@model = model
@reference = reference
@column = column
@title = !!title
end
|
Instance Attribute Details
#column ⇒ Object
Returns the value of attribute column.
11
12
13
|
# File 'lib/model_base/column_attribute.rb', line 11
def column
@column
end
|
#linkable ⇒ Object
Also known as:
linkable?
Returns the value of attribute linkable.
7
8
9
|
# File 'lib/model_base/column_attribute.rb', line 7
def linkable
@linkable
end
|
#model ⇒ Object
Returns the value of attribute model.
11
12
13
|
# File 'lib/model_base/column_attribute.rb', line 11
def model
@model
end
|
#reference ⇒ Object
Returns the value of attribute reference.
10
11
12
|
# File 'lib/model_base/column_attribute.rb', line 10
def reference
@reference
end
|
#title=(value) ⇒ Object
24
25
26
|
# File 'lib/model_base/column_attribute.rb', line 24
def title=(value)
@title = value
end
|
Instance Method Details
#assert_select_exp ⇒ Object
121
122
123
124
125
126
127
128
129
|
# File 'lib/model_base/column_attribute.rb', line 121
def assert_select_exp
model_name = model.full_resource_name
case type
when :datetime, :timestamp, :time
"assert_select_datetime_field :#{model_name}, :#{name}"
else
"assert_select '#{ input_type }##{ model_name }_#{ name }[name=?]', '#{ model_name }[#{ name }]'"
end
end
|
#base_sample_value ⇒ Object
102
103
104
|
# File 'lib/model_base/column_attribute.rb', line 102
def base_sample_value
name.split('').map(&:ord).sum
end
|
#enumerized? ⇒ Boolean
37
38
39
40
|
# File 'lib/model_base/column_attribute.rb', line 37
def enumerized?
model.model_class.respond_to?(name) &&
defined?(Enumerize) && model.model_class.send(name).is_a?(Enumerize::Attribute)
end
|
47
48
49
|
# File 'lib/model_base/column_attribute.rb', line 47
def input_type
select_renderer ? :select : super
end
|
#new_attribute_exp ⇒ Object
144
145
146
147
148
149
150
151
152
|
# File 'lib/model_base/column_attribute.rb', line 144
def new_attribute_exp
if enumerized?
'%s.%s.values[%d]' % [model.name, name, 1]
elsif type == :boolean
"valid_parameters[:#{name}].!"
else
"valid_parameters[:#{name}].succ"
end
end
|
#ref_model ⇒ Object
26
27
28
29
30
31
|
# File 'lib/model_base/column_attribute.rb', line 26
def ref_model
unless defined?(@ref_model)
@ref_model = reference.nil? ? nil : ModelBase::MetaModel.new(reference.class_name)
end
@ref_model
end
|
#required? ⇒ Boolean
33
34
35
|
# File 'lib/model_base/column_attribute.rb', line 33
def required?
column ? !column.null : false
end
|
#sample_string_exp(idx = 1) ⇒ Object
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/model_base/column_attribute.rb', line 131
def sample_string_exp(idx = 1)
case type
when :datetime, :timestamp, :time
'localize(Time.zone.parse(\'%s\'))' % sample_value(idx)
else
if !title? && ref_model && !ref_model.title_column
"#{ref_model.full_resource_name}.title"
else
"'%s'" % sample_value(idx)
end
end
end
|
#sample_time(idx = 1) ⇒ Object
106
107
108
109
110
|
# File 'lib/model_base/column_attribute.rb', line 106
def sample_time(idx = 1)
ModelBase.base_time +
model.sample_value.hours +
(base_sample_value.minutes * 10 * idx)
end
|
#sample_value(idx = 1, context: nil) ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/model_base/column_attribute.rb', line 64
def sample_value(idx = 1, context: nil)
if name == 'id'
idx
elsif name == 'email' && type == :string
'[email protected]'
elsif title?
model.full_resource_name + idx.to_s
elsif ref_model
if tc = ref_model.title_column
tc.sample_value(idx)
else
block_given? ? yield : 1
end
elsif enumerized?
enum = model.model_class.send(name)
r = enum.values.first
context == :factory ? r.to_sym : r.text
else
case type
when :integer then idx
when :float then idx + 0.5
when :decimal then "#{idx}.99"
when :datetime, :timestamp, :time then sample_time(idx).strftime('%F %T %:z')
when :date then sample_time(idx).to_date.to_s(:db)
when :string then
case name
when 'type' then ""
else "#{model.full_resource_name}_#{name}_#{idx}"
end
when :text then "#{model.full_resource_name}_#{name}_#{idx}"
when :boolean then false
when :references, :belongs_to then nil
else
""
end
end
end
|
#sample_value_regexp_exp(idx = 1) ⇒ Object
112
113
114
115
116
117
118
119
|
# File 'lib/model_base/column_attribute.rb', line 112
def sample_value_regexp_exp(idx = 1)
case type
when :datetime, :timestamp, :time
"localized_time_re('%s')" % sample_value(idx)
else
'/%s/' % sample_value(idx)
end
end
|
#select_renderer ⇒ Object
42
43
44
45
|
# File 'lib/model_base/column_attribute.rb', line 42
def select_renderer
ref_model ? ReferenceSelectRenderer.new(self) :
enumerized? ? EnumerizedSelectRenderer.new(self) : nil
end
|
#single_sample_only? ⇒ Boolean
56
57
58
59
60
61
62
|
# File 'lib/model_base/column_attribute.rb', line 56
def single_sample_only?
ref_model || enumerized? ||
case type
when :boolean, :datetime, :timestamp, :time, :date then true
else false
end
end
|
#title? ⇒ Boolean
21
22
23
|
# File 'lib/model_base/column_attribute.rb', line 21
def title?
@title
end
|
#to_be_localized? ⇒ Boolean
52
53
54
|
# File 'lib/model_base/column_attribute.rb', line 52
def to_be_localized?
LOCALIZED_TYPES.include?(type)
end
|