cck_forms 3.0.1 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +254 -1
- data/lib/cck_forms/date_time.rb +4 -0
- data/lib/cck_forms/engine.rb +2 -2
- data/lib/cck_forms/form_builder_extensions.rb +2 -1
- data/lib/cck_forms/neofiles_denormalize.rb +71 -0
- data/lib/cck_forms/parameter_type_class/album.rb +15 -11
- data/lib/cck_forms/parameter_type_class/base.rb +58 -63
- data/lib/cck_forms/parameter_type_class/boolean.rb +6 -0
- data/lib/cck_forms/parameter_type_class/checkboxes.rb +31 -15
- data/lib/cck_forms/parameter_type_class/date.rb +5 -0
- data/lib/cck_forms/parameter_type_class/date_range.rb +8 -2
- data/lib/cck_forms/parameter_type_class/date_time.rb +10 -8
- data/lib/cck_forms/parameter_type_class/enum.rb +11 -0
- data/lib/cck_forms/parameter_type_class/file.rb +27 -20
- data/lib/cck_forms/parameter_type_class/float.rb +3 -0
- data/lib/cck_forms/parameter_type_class/image.rb +3 -1
- data/lib/cck_forms/parameter_type_class/integer.rb +10 -4
- data/lib/cck_forms/parameter_type_class/integer_range.rb +18 -0
- data/lib/cck_forms/parameter_type_class/map.rb +23 -23
- data/lib/cck_forms/parameter_type_class/phones.rb +14 -15
- data/lib/cck_forms/parameter_type_class/string.rb +3 -0
- data/lib/cck_forms/parameter_type_class/string_collection.rb +6 -0
- data/lib/cck_forms/parameter_type_class/text.rb +4 -0
- data/lib/cck_forms/parameter_type_class/time.rb +4 -0
- data/lib/cck_forms/parameter_type_class/work_hours.rb +24 -36
- data/lib/cck_forms/version.rb +1 -1
- data/vendor/assets/javascripts/cck_forms/index.js +2 -0
- metadata +4 -2
@@ -1,53 +1,46 @@
|
|
1
|
-
#
|
2
|
-
# например, String, Checkboxes и т. п.
|
1
|
+
# Base module for all field types. Is included in type classes like Album, Image etc.
|
3
2
|
#
|
4
3
|
# class CckForms::ParameterTypeClass::NewType
|
5
4
|
# include CckForms::ParameterTypeClass::Base
|
6
|
-
#
|
7
|
-
# def self.name
|
8
|
-
# 'Новый тип'
|
9
|
-
# end
|
10
5
|
# end
|
11
6
|
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
# Использование типов:
|
7
|
+
# Standalone usage of real classes:
|
15
8
|
#
|
16
9
|
# field :cover_photo, type: CckForms::ParameterTypeClass::Image
|
17
|
-
# field :gallery,
|
10
|
+
# field :gallery, type: CckForms::ParameterTypeClass::Album
|
18
11
|
# field :description, type: CckForms::ParameterTypeClass::Text
|
19
12
|
#
|
20
|
-
#
|
13
|
+
# Base module includes:
|
21
14
|
#
|
22
|
-
# 1)
|
15
|
+
# 1) URL helpers like edit_article_path accessible via include Rails.application.routes.url_helpers;
|
23
16
|
#
|
24
|
-
# 2)
|
17
|
+
# 2) methods cck_param & value returning current CCK parameter (module CckForms::*::Parameter) and his current value;
|
25
18
|
#
|
26
|
-
# 3)
|
27
|
-
#
|
19
|
+
# 3) methods with_cck_param(param) do ..., with_value(value) do ... and with_cck_param_and_value(param, value) do ...
|
20
|
+
# which set the corresponding values for the block duration;
|
28
21
|
#
|
29
|
-
# 4)
|
30
|
-
#
|
22
|
+
# 4) dynamic method (via method_missing & respond_to?) ..._with_value(value, args*) which is basically the same as
|
23
|
+
# with_value do... but for one method invocation only: foo_with_value(v) <=> with_value(v) { foo }
|
31
24
|
#
|
32
|
-
# 5) set_value_in_hash(hash)
|
25
|
+
# 5) utility set_value_in_hash(hash) placing value in hash[:value];
|
33
26
|
#
|
34
|
-
# 6)
|
27
|
+
# 6) utilities to get HTML ID: form(_builder)?_name_to_id;
|
35
28
|
#
|
36
|
-
# 7)
|
29
|
+
# 7) methods to be consumed by the type classes:
|
37
30
|
#
|
38
|
-
# self.code
|
39
|
-
# self.name
|
31
|
+
# self.code - type code (e.g. rich_text for CckForms::ParameterType::RichText)
|
32
|
+
# self.name - type name (e.g. "A text string")
|
40
33
|
#
|
41
34
|
module CckForms::ParameterTypeClass::Base
|
42
35
|
extend ActiveSupport::Concern
|
43
36
|
|
44
37
|
included do
|
45
|
-
# Кое-где понадобятся помогайки-пути, сразу их включим.
|
46
38
|
include Rails.application.routes.url_helpers
|
47
39
|
|
48
40
|
attr_accessor :value
|
49
41
|
attr_reader :valid_values_class_name, :cck_parameter
|
50
42
|
|
43
|
+
# Store options into instance variables like @valid_values and so on
|
51
44
|
def initialize(options)
|
52
45
|
options = options.symbolize_keys
|
53
46
|
|
@@ -70,14 +63,20 @@ module CckForms::ParameterTypeClass::Base
|
|
70
63
|
|
71
64
|
|
72
65
|
module ClassMethods
|
66
|
+
# Called on a class to construct its instance from a MongoDB Hash.
|
67
|
+
# By default simply create a new object.
|
73
68
|
def demongoize(something_from_database)
|
74
69
|
new value: demongoize_value(something_from_database)
|
75
70
|
end
|
76
71
|
|
72
|
+
# Only converts a value from MongoDB to its in-memory (Ruby) form.
|
73
|
+
# By default, return the value itself.
|
77
74
|
def demongoize_value(value, parameter_type_class=nil)
|
78
75
|
value
|
79
76
|
end
|
80
77
|
|
78
|
+
# Called on an class to get a MongoDB Hash form of an instance object.
|
79
|
+
# By default simply calls mongoize of the instance.
|
81
80
|
def mongoize(object)
|
82
81
|
case object
|
83
82
|
when self then object.mongoize
|
@@ -87,43 +86,38 @@ module CckForms::ParameterTypeClass::Base
|
|
87
86
|
end
|
88
87
|
end
|
89
88
|
|
90
|
-
#
|
91
|
-
#
|
92
|
-
# различается, мы вынуждены отдельно описывать операцию emit, там где это необходимо. Смысл в том, чтобы этот метод
|
93
|
-
# вызвал emit для каждого хранимого значения.
|
89
|
+
# Returns Javascript emit function body to be used as a part of map/reduce "emit" step, see
|
90
|
+
# http://docs.mongodb.org/manual/applications/map-reduce/
|
94
91
|
#
|
95
|
-
#
|
96
|
-
#
|
97
|
-
#
|
98
|
-
# оно содержит список городов. Мы должны вызывать emit для каждого элемента массива (т. е., для каждого идентификатора
|
99
|
-
# города). Генерацией этого кода и занимается этот метод.
|
92
|
+
# The reason for this is every type class has its own notion of "current value" and stores it specifically. Say,
|
93
|
+
# Checkboxes store an array of values and if we want to get distinct values we need a way to extract each "single
|
94
|
+
# value" from this array.
|
100
95
|
#
|
101
|
-
#
|
102
|
-
#
|
96
|
+
# Example: imagine a field "city" of type Checkboxes. To make an aggregate query (a subtype of map-reduce)
|
97
|
+
# to count objects in different cities, for example, we can not run emit for the field as a whole since it is an
|
98
|
+
# array. We must call emit for each array value, that is for each city ID. This method does exactly this.
|
103
99
|
#
|
104
|
-
#
|
100
|
+
# In particular, it is used in counting popular values (like "give me a list of cities sorted by the number of
|
101
|
+
# host objects (ads? companies?) in them").
|
105
102
|
#
|
106
|
-
#
|
103
|
+
# By default considers a value in "#{feild_name}" atomic and call emit for it.
|
107
104
|
def emit_map_reduce(feild_name)
|
108
105
|
field_name = 'this.' + feild_name
|
109
106
|
"if(#{field_name} && #{field_name} != '') emit(#{field_name}, 1)"
|
110
107
|
end
|
111
108
|
|
112
|
-
#
|
109
|
+
# Converts input name intp HTML ID, e.g. facility[cck_params][1][value] -> facility_cck_params_1_value.
|
113
110
|
def form_name_to_id(name)
|
114
111
|
name.gsub(/\]\[|[^-a-zA-Z0-9:.]/, '_').sub(/_\z/, '')
|
115
112
|
end
|
116
113
|
|
117
|
-
# Методы, которые нужны наследникам (CckForms::ParameterTypeClass::*) для отображения, сортировки и прочей настройки самих типов.
|
118
|
-
# Эти методы не нужны обычным объектам - пользователям типов.
|
119
|
-
|
120
114
|
# CckForms::ParameterTypeClass::Checkboxes -> 'checkboxes'
|
121
115
|
# CckForms::ParameterTypeClass::RichText -> 'rich_text'
|
122
116
|
def code
|
123
117
|
self.to_s.demodulize.underscore
|
124
118
|
end
|
125
119
|
|
126
|
-
#
|
120
|
+
# A type name, e.g. "A text string"
|
127
121
|
def name
|
128
122
|
nil
|
129
123
|
end
|
@@ -131,7 +125,7 @@ module CckForms::ParameterTypeClass::Base
|
|
131
125
|
|
132
126
|
|
133
127
|
|
134
|
-
#
|
128
|
+
# Load all type classes
|
135
129
|
# TODO: relies on all classes to reside in this class' directory
|
136
130
|
def self.load_type_classes
|
137
131
|
return if @type_classes_loaded
|
@@ -146,10 +140,9 @@ module CckForms::ParameterTypeClass::Base
|
|
146
140
|
|
147
141
|
|
148
142
|
|
149
|
-
#
|
143
|
+
# Usual methods available to type classes (consumers of this module)
|
150
144
|
|
151
|
-
#
|
152
|
-
# Нужен для построения ХТМЛ СЕЛЕКТов.
|
145
|
+
# Generates valid_values in form consumable to SELECT helper builders: [[key1, value1], [key2, value2], ...]
|
153
146
|
def valid_values_enum
|
154
147
|
valid_values = self.valid_values
|
155
148
|
return [] if valid_values.blank?
|
@@ -161,13 +154,12 @@ module CckForms::ParameterTypeClass::Base
|
|
161
154
|
result
|
162
155
|
end
|
163
156
|
|
164
|
-
#
|
165
|
-
# Нужен для построения ХТМЛа и строк.
|
157
|
+
# Generates valid values as a comma-separated string: "georgian: грузинская, albanian: албанская" (for HTML puproses)
|
166
158
|
def valid_values_as_string
|
167
159
|
valid_values_enum.map { |enum| "#{enum[1]}: #{enum[0]}" }.join "\n"
|
168
160
|
end
|
169
161
|
|
170
|
-
#
|
162
|
+
# Convert HTML form back into Hash again:
|
171
163
|
# = f.text_field :valid_values_as_string
|
172
164
|
def valid_values_as_string=(string)
|
173
165
|
new_valid_values = {}
|
@@ -178,13 +170,12 @@ module CckForms::ParameterTypeClass::Base
|
|
178
170
|
self.valid_values = new_valid_values
|
179
171
|
end
|
180
172
|
|
181
|
-
# Вернет класс, указанный как valid_values_class_name.
|
182
173
|
# "City" -> City
|
183
174
|
def valid_values_class
|
184
175
|
if valid_values_class_name.present?
|
185
176
|
if valid_values_class_name.is_a? Class
|
186
177
|
valid_values_class_name
|
187
|
-
else #
|
178
|
+
else # raises exception if this is not a string
|
188
179
|
valid_values_class_name.constantize
|
189
180
|
end
|
190
181
|
else
|
@@ -192,13 +183,13 @@ module CckForms::ParameterTypeClass::Base
|
|
192
183
|
end
|
193
184
|
end
|
194
185
|
|
195
|
-
#
|
186
|
+
# Is valid_values_class exist at all?
|
196
187
|
def valid_values_class?
|
197
188
|
not valid_values_class.nil?
|
198
189
|
end
|
199
190
|
|
200
|
-
#
|
201
|
-
#
|
191
|
+
# If valid_values is empty and valid_values_class is not, extracts all values from this class into valid_values.
|
192
|
+
# Makes use of ActiveRecord-like method .all for this
|
202
193
|
def valid_values
|
203
194
|
@valid_values ||= begin
|
204
195
|
if vv_class = valid_values_class
|
@@ -209,39 +200,43 @@ module CckForms::ParameterTypeClass::Base
|
|
209
200
|
end
|
210
201
|
end
|
211
202
|
|
212
|
-
#
|
213
|
-
# options[:value]).
|
203
|
+
# Builds an edit form in HTML.
|
204
|
+
# By default an input:text (value can be set via options[:value]).
|
214
205
|
def build_form(form_builder, options)
|
215
206
|
set_value_in_hash options
|
216
207
|
form_builder.text_field :value, options
|
217
208
|
end
|
218
209
|
|
219
|
-
#
|
210
|
+
# HTML form of a type class.
|
211
|
+
# By default to_s.
|
220
212
|
def to_html(options = nil)
|
221
213
|
to_s options
|
222
214
|
end
|
223
215
|
|
224
|
-
#
|
216
|
+
# Redefines to allow passing of options.
|
217
|
+
# By default, call to_s on the current value.
|
225
218
|
def to_s(options = nil)
|
226
219
|
value.to_s
|
227
220
|
end
|
228
221
|
|
229
|
-
#
|
222
|
+
# Was-became HTML for admin panels etc. (like a type image with map preview for Map).
|
230
223
|
def to_diff_value(options = nil)
|
231
224
|
to_html options
|
232
225
|
end
|
233
226
|
|
234
|
-
#
|
227
|
+
# Transforms DSL-like query (specific to each type) into Mongoid query.
|
228
|
+
# By default use simple where(field: query.to_s).
|
235
229
|
def search(selectable, field, query)
|
236
230
|
selectable.where(field => query.to_s)
|
237
231
|
end
|
238
232
|
|
239
|
-
#
|
233
|
+
# For Rails.application.routes.url_helpers
|
240
234
|
def default_url_options
|
241
235
|
{}
|
242
236
|
end
|
243
237
|
|
244
|
-
#
|
238
|
+
# Transforms the value into MongoDB form.
|
239
|
+
# By default returns the value itself.
|
245
240
|
def mongoize
|
246
241
|
value
|
247
242
|
end
|
@@ -263,12 +258,12 @@ module CckForms::ParameterTypeClass::Base
|
|
263
258
|
options[:value] = value unless options.has_key? :value
|
264
259
|
end
|
265
260
|
|
266
|
-
#
|
261
|
+
# See ClassMethod.form_name_to_id
|
267
262
|
def form_name_to_id(name)
|
268
263
|
self.class.form_name_to_id name
|
269
264
|
end
|
270
265
|
|
271
|
-
#
|
266
|
+
# Converts FormBuilder name to ID, see form_name_to_id
|
272
267
|
def form_builder_name_to_id(form_builder, suffix = '')
|
273
268
|
form_name_to_id([form_builder.options[:namespace], form_builder.object_name].compact.join('_') + suffix)
|
274
269
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# Represents a single checkbox.
|
2
|
+
#
|
1
3
|
class CckForms::ParameterTypeClass::Boolean
|
2
4
|
include CckForms::ParameterTypeClass::Base
|
3
5
|
|
@@ -5,18 +7,22 @@ class CckForms::ParameterTypeClass::Boolean
|
|
5
7
|
'Галочка (да/нет)'
|
6
8
|
end
|
7
9
|
|
10
|
+
# Is it true?
|
8
11
|
def value?
|
9
12
|
value.present? && value != '0'
|
10
13
|
end
|
11
14
|
|
15
|
+
# Anything -> boolean
|
12
16
|
def mongoize
|
13
17
|
value?
|
14
18
|
end
|
15
19
|
|
20
|
+
# 'yes/no' string
|
16
21
|
def to_s(options = nil)
|
17
22
|
value? ? 'да' : 'нет'
|
18
23
|
end
|
19
24
|
|
25
|
+
# Checkbox HTML
|
20
26
|
def build_form(form_builder, options)
|
21
27
|
set_value_in_hash options
|
22
28
|
form_builder.check_box :value, options.merge(value: 1, checked: value?)
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# Represents a series of checkboxes. The values are take from valid_values, see base.rb
|
2
|
+
#
|
1
3
|
class CckForms::ParameterTypeClass::Checkboxes
|
2
4
|
include CckForms::ParameterTypeClass::Base
|
3
5
|
|
@@ -25,7 +27,7 @@ class CckForms::ParameterTypeClass::Checkboxes
|
|
25
27
|
end
|
26
28
|
|
27
29
|
# ['kazakh', 'english'] -> {'kazakh' => 1, 'russain' => 0, 'english' => 1}
|
28
|
-
# (
|
30
|
+
# (for for builders)
|
29
31
|
def self.demongoize_value(value, parameter_type_class=nil)
|
30
32
|
value.present? or return {}
|
31
33
|
|
@@ -36,13 +38,14 @@ class CckForms::ParameterTypeClass::Checkboxes
|
|
36
38
|
end
|
37
39
|
end
|
38
40
|
|
39
|
-
#
|
40
|
-
#
|
41
|
-
#
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
45
|
-
#
|
41
|
+
# Comma-separated list of checked entries. Options:
|
42
|
+
#
|
43
|
+
# block - sprintf template for each entry
|
44
|
+
# only - leave only these keys (array/string)
|
45
|
+
# except - reverse of :only
|
46
|
+
# glue - use instead of ', ' for .join
|
47
|
+
# short - make the string shorter, if possible: if all variants are checked, returns string "all selected"
|
48
|
+
# if few are not checked, returns "all except xxx, yyy, ..."
|
46
49
|
def to_s(options = nil)
|
47
50
|
checked_keys, checked_elements = [], []
|
48
51
|
return '' if value.blank?
|
@@ -92,6 +95,10 @@ class CckForms::ParameterTypeClass::Checkboxes
|
|
92
95
|
checked_elements.join glue
|
93
96
|
end
|
94
97
|
|
98
|
+
# Construct Mongoid query from our internal. If query is a Hash, find all objects where field has ALL of the Hash keys.
|
99
|
+
# Key 'all' selected all objects.
|
100
|
+
#
|
101
|
+
# Otherwise, use usual where(field => query).
|
95
102
|
def search(selectable, field, query)
|
96
103
|
if query.respond_to? :each_pair
|
97
104
|
keys = []
|
@@ -112,11 +119,11 @@ class CckForms::ParameterTypeClass::Checkboxes
|
|
112
119
|
selectable
|
113
120
|
end
|
114
121
|
|
115
|
-
#
|
122
|
+
# Construct emit func for map/reduce to emit on each array value in MongoDB. For example, for object:
|
116
123
|
#
|
117
124
|
# cck_params.city: ['almaty', 'astana']
|
118
125
|
#
|
119
|
-
#
|
126
|
+
# we will emit('almaty', 1); emit('astana', 1).
|
120
127
|
def self.emit_map_reduce(field_name)
|
121
128
|
field_name = 'this.' + field_name
|
122
129
|
return "if(#{field_name} && #{field_name} instanceof Array) {
|
@@ -126,11 +133,15 @@ class CckForms::ParameterTypeClass::Checkboxes
|
|
126
133
|
}"
|
127
134
|
end
|
128
135
|
|
129
|
-
# options
|
130
|
-
#
|
131
|
-
#
|
132
|
-
#
|
133
|
-
#
|
136
|
+
# options:
|
137
|
+
#
|
138
|
+
# block - sprintf template for each entry (input, label)
|
139
|
+
# map - convert entry labels, e.g. 'long label text' => 'sh.txt'
|
140
|
+
# data - data-attrs for label, Hash (e.g. capital: {almaty: 'yes', astana: 'no'})
|
141
|
+
# as - if :select, construct a SELECT, not a checkboxes list
|
142
|
+
# only - see #to_s
|
143
|
+
# except - see #to_s
|
144
|
+
# for - if :search, do not add false-value checkbox
|
134
145
|
def build_form(form_builder, options)
|
135
146
|
return '' unless valid_values.is_a?(Hash) || valid_values.is_a?(Array)
|
136
147
|
|
@@ -176,6 +187,11 @@ class CckForms::ParameterTypeClass::Checkboxes
|
|
176
187
|
|
177
188
|
private
|
178
189
|
|
190
|
+
# options:
|
191
|
+
#
|
192
|
+
# only - see #to_s
|
193
|
+
# except - see #to_s
|
194
|
+
# required - HTML required attr
|
179
195
|
def build_select_form(form_builder, options)
|
180
196
|
set_value_in_hash options
|
181
197
|
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# Represents a single date.
|
2
|
+
#
|
1
3
|
class CckForms::ParameterTypeClass::Date
|
2
4
|
include CckForms::ParameterTypeClass::Base
|
3
5
|
include CckForms::DateTime
|
@@ -6,11 +8,13 @@ class CckForms::ParameterTypeClass::Date
|
|
6
8
|
'Дата'
|
7
9
|
end
|
8
10
|
|
11
|
+
# Date SELECT
|
9
12
|
def build_form(form_builder, options)
|
10
13
|
set_value_in_hash options
|
11
14
|
self.class.build_date_form(form_builder, options)
|
12
15
|
end
|
13
16
|
|
17
|
+
# Date SELECT as a set of 3 SELECTS: year, month, date
|
14
18
|
def self.build_date_form(form_builder, options, type = '')
|
15
19
|
val = options[:value].is_a?(Hash) ? options[:value][type] : options[:value]
|
16
20
|
val = CckForms::ParameterTypeClass::Time::date_object_from_what_stored_in_database(val)
|
@@ -19,6 +23,7 @@ class CckForms::ParameterTypeClass::Date
|
|
19
23
|
('<div class="form-inline">%s</div>' % form_builder.fields_for(:value) { |datetime_builder| datetime_builder.date_select type, form_element_options, form_element_html}).html_safe
|
20
24
|
end
|
21
25
|
|
26
|
+
# "12.12.2012"
|
22
27
|
def to_s(options = nil)
|
23
28
|
if value.is_a? Time
|
24
29
|
the_value = {
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# Represents a pair of date SELECTs (date range).
|
2
|
+
#
|
1
3
|
class CckForms::ParameterTypeClass::DateRange
|
2
4
|
include CckForms::ParameterTypeClass::Base
|
3
5
|
|
@@ -5,6 +7,7 @@ class CckForms::ParameterTypeClass::DateRange
|
|
5
7
|
'Диапазон между двумя датами'
|
6
8
|
end
|
7
9
|
|
10
|
+
# Converts input hash of type {from: ..., till: ...} to MongoDB format
|
8
11
|
def mongoize
|
9
12
|
value_from_form = value
|
10
13
|
return nil if value_from_form.blank?
|
@@ -21,6 +24,7 @@ class CckForms::ParameterTypeClass::DateRange
|
|
21
24
|
db_representation
|
22
25
|
end
|
23
26
|
|
27
|
+
# 2 date SELECTs
|
24
28
|
def build_form(form_builder, options)
|
25
29
|
result = []
|
26
30
|
set_value_in_hash options
|
@@ -32,6 +36,9 @@ class CckForms::ParameterTypeClass::DateRange
|
|
32
36
|
result.join.html_safe
|
33
37
|
end
|
34
38
|
|
39
|
+
# "from 21.12.2012"
|
40
|
+
# "till 22.12.2012"
|
41
|
+
# "21.12.2012 - 22.12.2012"
|
35
42
|
def to_s
|
36
43
|
return '' unless value.present? && value.is_a?(Hash)
|
37
44
|
types = {}
|
@@ -49,5 +56,4 @@ class CckForms::ParameterTypeClass::DateRange
|
|
49
56
|
end
|
50
57
|
|
51
58
|
end
|
52
|
-
|
53
|
-
end
|
59
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# Represents a single date & time.
|
2
|
+
#
|
1
3
|
class CckForms::ParameterTypeClass::DateTime
|
2
4
|
include CckForms::ParameterTypeClass::Base
|
3
5
|
include CckForms::DateTime
|
@@ -6,6 +8,7 @@ class CckForms::ParameterTypeClass::DateTime
|
|
6
8
|
'Дата и время'
|
7
9
|
end
|
8
10
|
|
11
|
+
# Date and time SELECTs
|
9
12
|
def build_form(form_builder, options)
|
10
13
|
set_value_in_hash options
|
11
14
|
value = CckForms::ParameterTypeClass::Time::date_object_from_what_stored_in_database(options[:value])
|
@@ -15,19 +18,18 @@ class CckForms::ParameterTypeClass::DateTime
|
|
15
18
|
('<div class="form-inline">%s</div>' % form_builder.fields_for(:value) { |datetime_builder| datetime_builder.datetime_select '', form_element_options, form_element_html})
|
16
19
|
end
|
17
20
|
|
18
|
-
#
|
19
|
-
# например:
|
21
|
+
# Options is a :symbol -> options[:symbol] = true:
|
20
22
|
#
|
21
23
|
# date_attr.to_s :only_date
|
22
|
-
# date_attr.to_s :only_date => true #
|
24
|
+
# date_attr.to_s :only_date => true # equivalent
|
23
25
|
#
|
24
|
-
#
|
26
|
+
# options:
|
25
27
|
#
|
26
|
-
# year_obligatory -
|
27
|
-
# only_date -
|
28
|
-
# rus_date -
|
28
|
+
# year_obligatory - force year in output (by default current year is skipped)
|
29
|
+
# only_date - hide time
|
30
|
+
# rus_date - Russian date, like "2 июля"
|
29
31
|
#
|
30
|
-
#
|
32
|
+
# By default: "01.02.2012, 12:49".
|
31
33
|
def to_s(options=nil)
|
32
34
|
value = if self.value.is_a? Time
|
33
35
|
{
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# Represents a enum (SELECT).
|
2
|
+
#
|
1
3
|
class CckForms::ParameterTypeClass::Enum
|
2
4
|
include CckForms::ParameterTypeClass::Base
|
3
5
|
|
@@ -9,11 +11,14 @@ class CckForms::ParameterTypeClass::Enum
|
|
9
11
|
value.presence
|
10
12
|
end
|
11
13
|
|
14
|
+
# Current string value from valid_values
|
12
15
|
def to_s(options = nil)
|
13
16
|
return '' if value.blank?
|
14
17
|
valid_values[value].to_s
|
15
18
|
end
|
16
19
|
|
20
|
+
# Constructs MongoDB query. If query is a Hash, find all objects where field is any of the hash keys, for which value
|
21
|
+
# is '1'. Otherwise treat query as where(filed => query).
|
17
22
|
def search(selectable, field, query)
|
18
23
|
if query.is_a? Hash
|
19
24
|
query = query.map { |k, v| v == '1' ? k : nil }.compact
|
@@ -27,6 +32,12 @@ class CckForms::ParameterTypeClass::Enum
|
|
27
32
|
end
|
28
33
|
end
|
29
34
|
|
35
|
+
# options:
|
36
|
+
#
|
37
|
+
# as - if 'checkboxes', display a set of checkboxes, not SELECT
|
38
|
+
# only - leave only these keys (array/string)
|
39
|
+
# except - reverse of :only
|
40
|
+
# required - HTML required attr
|
30
41
|
def build_form(form_builder, options)
|
31
42
|
|
32
43
|
if options.is_a? Hash and options[:as] == 'checkboxes'
|
@@ -1,5 +1,8 @@
|
|
1
|
+
# Represents a single file.
|
2
|
+
#
|
1
3
|
class CckForms::ParameterTypeClass::File
|
2
4
|
include CckForms::ParameterTypeClass::Base
|
5
|
+
include CckForms::NeofilesDenormalize
|
3
6
|
|
4
7
|
def self.name
|
5
8
|
'Файл'
|
@@ -13,29 +16,23 @@ class CckForms::ParameterTypeClass::File
|
|
13
16
|
self.class.file_type
|
14
17
|
end
|
15
18
|
|
16
|
-
#
|
19
|
+
# Converts Neofiles::File or its ID into denormalized Hash to be stored in MongoDB
|
17
20
|
def mongoize
|
18
|
-
|
19
|
-
when file_type then value.id
|
20
|
-
when ::String then value
|
21
|
-
end
|
21
|
+
self.class.neofiles_attrs_or_id value, file_type
|
22
22
|
end
|
23
23
|
|
24
|
-
#
|
24
|
+
# Converts denormalized attrs hash or ID to Neofiles::File instance (possibly lazy loadable)
|
25
25
|
def self.demongoize_value(value, parameter_type_class=nil)
|
26
|
-
|
27
|
-
rescue Mongoid::Errors::DocumentNotFound
|
28
|
-
nil
|
26
|
+
neofiles_mock_or_load value
|
29
27
|
end
|
30
28
|
|
31
|
-
#
|
32
|
-
#
|
33
|
-
# Ставит ДИВ и делает аяксовый вызов метода file_compact контроллера Neofiles::AdminController.
|
29
|
+
# Constructs HTML form for image upload & manipulation. Basically, it is a DIV with some ID and a Javascript widget
|
30
|
+
# creation for that DIV.
|
34
31
|
#
|
35
|
-
#
|
36
|
-
#
|
37
|
-
# value - текущее значение (идентификатор или объект Neofiles::File)
|
32
|
+
# options:
|
38
33
|
#
|
34
|
+
# value - current value (ID or Neofiles::File)
|
35
|
+
# with_desc - if true, show the description edit richtext (default false)
|
39
36
|
def build_form(form_builder, options)
|
40
37
|
set_value_in_hash options
|
41
38
|
self.class.create_load_form helper: self,
|
@@ -45,15 +42,23 @@ class CckForms::ParameterTypeClass::File
|
|
45
42
|
with_desc: options[:with_desc]
|
46
43
|
end
|
47
44
|
|
45
|
+
# Create image load DIV & script. A separate function to allow other (non-CCK) fields to utilize this functionality.
|
46
|
+
#
|
47
|
+
# helper - view context for HTML generation (`self` in views or `view_context` in controllers)
|
48
|
+
# file - Neofiles::File or ID
|
49
|
+
# widget_id - DIV ID
|
50
|
+
# input_name, append_create, clean_remove, disabled, multiple, with_desc
|
51
|
+
# - Neofiles arguments, see Neofiles::AdminController
|
48
52
|
def self.create_load_form(helper:, file:, input_name:, append_create: false, clean_remove: false, widget_id: nil, disabled: false, multiple: false, with_desc: false)
|
49
53
|
cont_id = 'container_' + (widget_id.presence || form_name_to_id(input_name))
|
50
54
|
|
51
55
|
# создаем временное поле, чтобы пока аяксовый ответ не вернется, мы могли все же отправить родительскую форму
|
52
56
|
# и не потерять при этом данные из поля
|
57
|
+
file_id = file.is_a?(file_type) ? file.id.to_s : file.to_s
|
53
58
|
temp_field, remove_temp_field = '', ''
|
54
|
-
if
|
55
|
-
temp_id = "temp_file_field_#{
|
56
|
-
temp_field = '<input id="' + temp_id + '" type="hidden" name="' + input_name + '" value="' +
|
59
|
+
if file_id.present?
|
60
|
+
temp_id = "temp_file_field_#{file_id}"
|
61
|
+
temp_field = '<input id="' + temp_id + '" type="hidden" name="' + input_name + '" value="' + file_id + '">'
|
57
62
|
remove_temp_field = '$("#' + temp_id + '").remove();'
|
58
63
|
end
|
59
64
|
|
@@ -62,7 +67,7 @@ class CckForms::ParameterTypeClass::File
|
|
62
67
|
|
63
68
|
<script type="text/javascript">
|
64
69
|
$(function() {
|
65
|
-
$("#' + cont_id + '").load("' + helper.neofiles_file_compact_path(id:
|
70
|
+
$("#' + cont_id + '").load("' + helper.neofiles_file_compact_path(id: file_id, input_name: input_name, widget_id: widget_id, append_create: append_create ? '1' : nil, clean_remove: clean_remove ? '1' : nil, disabled: disabled ? '1' : nil, multiple: multiple ? '1' : nil, with_desc: with_desc ? '1' : nil) + '", null, function() {
|
66
71
|
$(this).children().unwrap();
|
67
72
|
' + remove_temp_field + '
|
68
73
|
});
|
@@ -70,11 +75,13 @@ class CckForms::ParameterTypeClass::File
|
|
70
75
|
</script>'
|
71
76
|
end
|
72
77
|
|
78
|
+
# Returns empty string
|
73
79
|
def to_s
|
74
80
|
''
|
75
81
|
end
|
76
82
|
|
83
|
+
# Returns a file name
|
77
84
|
def to_diff_value(options = {})
|
78
|
-
|
85
|
+
value.try! :name
|
79
86
|
end
|
80
87
|
end
|