locomotivecms_mounter_pull_19 1.5.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +4 -0
  5. data/Gemfile +7 -0
  6. data/Gemfile.lock +131 -0
  7. data/MIT-LICENSE +20 -0
  8. data/NOTES +4 -0
  9. data/README.md +26 -0
  10. data/Rakefile +37 -0
  11. data/TODO +82 -0
  12. data/init.rb +2 -0
  13. data/lib/locomotive/mounter.rb +106 -0
  14. data/lib/locomotive/mounter/config.rb +21 -0
  15. data/lib/locomotive/mounter/engine_api.rb +205 -0
  16. data/lib/locomotive/mounter/exceptions.rb +44 -0
  17. data/lib/locomotive/mounter/extensions/compass.rb +38 -0
  18. data/lib/locomotive/mounter/extensions/httmultiparty.rb +22 -0
  19. data/lib/locomotive/mounter/extensions/sprockets.rb +46 -0
  20. data/lib/locomotive/mounter/extensions/tilt/css.rb +31 -0
  21. data/lib/locomotive/mounter/fields.rb +254 -0
  22. data/lib/locomotive/mounter/models/base.rb +42 -0
  23. data/lib/locomotive/mounter/models/content_asset.rb +84 -0
  24. data/lib/locomotive/mounter/models/content_entry.rb +372 -0
  25. data/lib/locomotive/mounter/models/content_field.rb +190 -0
  26. data/lib/locomotive/mounter/models/content_select_option.rb +29 -0
  27. data/lib/locomotive/mounter/models/content_type.rb +274 -0
  28. data/lib/locomotive/mounter/models/editable_element.rb +27 -0
  29. data/lib/locomotive/mounter/models/page.rb +442 -0
  30. data/lib/locomotive/mounter/models/site.rb +28 -0
  31. data/lib/locomotive/mounter/models/snippet.rb +55 -0
  32. data/lib/locomotive/mounter/models/theme_asset.rb +148 -0
  33. data/lib/locomotive/mounter/models/translation.rb +28 -0
  34. data/lib/locomotive/mounter/mounting_point.rb +65 -0
  35. data/lib/locomotive/mounter/reader/api.rb +64 -0
  36. data/lib/locomotive/mounter/reader/api/base.rb +67 -0
  37. data/lib/locomotive/mounter/reader/api/content_assets_reader.rb +39 -0
  38. data/lib/locomotive/mounter/reader/api/content_entries_reader.rb +142 -0
  39. data/lib/locomotive/mounter/reader/api/content_types_reader.rb +76 -0
  40. data/lib/locomotive/mounter/reader/api/pages_reader.rb +192 -0
  41. data/lib/locomotive/mounter/reader/api/site_reader.rb +42 -0
  42. data/lib/locomotive/mounter/reader/api/snippets_reader.rb +61 -0
  43. data/lib/locomotive/mounter/reader/api/theme_assets_reader.rb +42 -0
  44. data/lib/locomotive/mounter/reader/api/translations_reader.rb +30 -0
  45. data/lib/locomotive/mounter/reader/file_system.rb +43 -0
  46. data/lib/locomotive/mounter/reader/file_system/base.rb +65 -0
  47. data/lib/locomotive/mounter/reader/file_system/content_assets_reader.rb +90 -0
  48. data/lib/locomotive/mounter/reader/file_system/content_entries_reader.rb +97 -0
  49. data/lib/locomotive/mounter/reader/file_system/content_types_reader.rb +88 -0
  50. data/lib/locomotive/mounter/reader/file_system/pages_reader.rb +211 -0
  51. data/lib/locomotive/mounter/reader/file_system/site_reader.rb +27 -0
  52. data/lib/locomotive/mounter/reader/file_system/snippets_reader.rb +115 -0
  53. data/lib/locomotive/mounter/reader/file_system/theme_assets_reader.rb +83 -0
  54. data/lib/locomotive/mounter/reader/file_system/translations_reader.rb +36 -0
  55. data/lib/locomotive/mounter/reader/runner.rb +89 -0
  56. data/lib/locomotive/mounter/utils/hash.rb +31 -0
  57. data/lib/locomotive/mounter/utils/output.rb +124 -0
  58. data/lib/locomotive/mounter/utils/string.rb +40 -0
  59. data/lib/locomotive/mounter/utils/yaml.rb +125 -0
  60. data/lib/locomotive/mounter/utils/yaml_front_matters_template.rb +45 -0
  61. data/lib/locomotive/mounter/version.rb +8 -0
  62. data/lib/locomotive/mounter/writer/api.rb +74 -0
  63. data/lib/locomotive/mounter/writer/api/base.rb +172 -0
  64. data/lib/locomotive/mounter/writer/api/content_assets_writer.rb +74 -0
  65. data/lib/locomotive/mounter/writer/api/content_entries_writer.rb +227 -0
  66. data/lib/locomotive/mounter/writer/api/content_types_writer.rb +151 -0
  67. data/lib/locomotive/mounter/writer/api/pages_writer.rb +250 -0
  68. data/lib/locomotive/mounter/writer/api/site_writer.rb +125 -0
  69. data/lib/locomotive/mounter/writer/api/snippets_writer.rb +111 -0
  70. data/lib/locomotive/mounter/writer/api/theme_assets_writer.rb +201 -0
  71. data/lib/locomotive/mounter/writer/api/translations_writer.rb +85 -0
  72. data/lib/locomotive/mounter/writer/file_system.rb +44 -0
  73. data/lib/locomotive/mounter/writer/file_system/base.rb +70 -0
  74. data/lib/locomotive/mounter/writer/file_system/content_assets_writer.rb +38 -0
  75. data/lib/locomotive/mounter/writer/file_system/content_entries_writer.rb +33 -0
  76. data/lib/locomotive/mounter/writer/file_system/content_types_writer.rb +32 -0
  77. data/lib/locomotive/mounter/writer/file_system/pages_writer.rb +93 -0
  78. data/lib/locomotive/mounter/writer/file_system/site_writer.rb +30 -0
  79. data/lib/locomotive/mounter/writer/file_system/snippets_writer.rb +59 -0
  80. data/lib/locomotive/mounter/writer/file_system/theme_assets_writer.rb +72 -0
  81. data/lib/locomotive/mounter/writer/file_system/translations_writer.rb +29 -0
  82. data/lib/locomotive/mounter/writer/runner.rb +73 -0
  83. data/locomotivecms_mounter.gemspec +64 -0
  84. metadata +539 -0
@@ -0,0 +1,190 @@
1
+ module Locomotive
2
+ module Mounter
3
+ module Models
4
+
5
+ class ContentField < Base
6
+
7
+ ## fields ##
8
+ field :label
9
+ field :name
10
+ field :type, default: :string
11
+ field :hint
12
+ field :position, default: 0
13
+ field :required, default: false
14
+ field :unique, default: false
15
+ field :localized, default: false
16
+ field :searchable, default: false
17
+
18
+ # text
19
+ field :text_formatting
20
+
21
+ # select
22
+ field :select_options, type: :array, class_name: 'Locomotive::Mounter::Models::ContentSelectOption'
23
+
24
+ # relationships: belongs_to, has_many, many_to_many
25
+ field :class_slug # out
26
+ field :class_name # in
27
+ field :inverse_of
28
+ field :order_by
29
+ field :ui_enabled
30
+
31
+ alias :target :class_name
32
+ alias :target= :class_name=
33
+ alias :raw_select_options= :select_options=
34
+
35
+ ## callbacks ##
36
+ set_callback :initialize, :after, :prepare_attributes
37
+
38
+ ## other accessors ##
39
+ attr_accessor :_destroy
40
+
41
+ ## methods ##
42
+
43
+ def prepare_attributes
44
+ self.label ||= self.name.try(:humanize)
45
+ self.type = self.type.to_sym
46
+ self.sanitize
47
+ end
48
+
49
+ # Tell if it describes a relationship (belongs_to, many_to_many, has_many) or not.
50
+ #
51
+ # @return [ Boolean ] True if describing a relationship
52
+ #
53
+ def is_relationship?
54
+ %w(belongs_to has_many many_to_many).include?(self.type.to_s)
55
+ end
56
+
57
+ # Tell if the id, the name of the field or other property based on
58
+ # its name (like formatted_<date field>) matches the parameter.
59
+ #
60
+ # @param [ String / Symbol] name_or_id Name or Id of the field
61
+ #
62
+ # @return [ Boolean ] True if the current field matches the parameter
63
+ #
64
+ def matches?(id_or_name)
65
+ default = [self._id, self.name]
66
+
67
+ list = default + (case self.type.to_sym
68
+ when :date, :date_time then ["formatted_#{self.name}"]
69
+ else
70
+ []
71
+ end)
72
+
73
+ list.include?(id_or_name.to_s)
74
+ end
75
+
76
+ # Return the content type matching the class_name / target attribute
77
+ #
78
+ # @return [ Object ] The matching Content Type
79
+ #
80
+ def klass
81
+ (@klass ||= self.mounting_point.content_types[self.class_name]).tap do |klass|
82
+ if klass.nil?
83
+ raise UnknownContentTypeException.new("unknow content type #{self.class_name}")
84
+ end
85
+ end
86
+ end
87
+
88
+ # Set directly the content type matching the class_name.
89
+ #
90
+ # @param [ Object ] The matching Content Type
91
+ #
92
+ def klass=(content_type)
93
+ if self.class_name == content_type.slug
94
+ @klass = content_type
95
+ end
96
+ end
97
+
98
+ # Find a select option by its name IN the current locale.
99
+ #
100
+ # @param [ String / Symbol] name_or_id Name or Id of the option
101
+ #
102
+ # @return [ Object ] The select option or nil if not found
103
+ #
104
+ def find_select_option(name_or_id)
105
+ return nil if self.select_options.blank?
106
+ self.select_options.detect { |option| option.name.to_s == name_or_id.to_s || option._id == name_or_id }
107
+ end
108
+
109
+ # Return the params used for the API.
110
+ #
111
+ # @return [ Hash ] The params
112
+ #
113
+ def to_params
114
+ params = self.filter_attributes %w(label name type hint position required localized unique searchable)
115
+
116
+ # we set the _id / _destroy attributes for embedded documents
117
+ params[:_id] = self._id if self.persisted?
118
+ params[:_destroy] = self._destroy if self._destroy
119
+
120
+ case self.type
121
+ when :text
122
+ params[:text_formatting] = self.text_formatting
123
+ when :select
124
+ params[:raw_select_options] = self.select_options.map(&:to_params)
125
+ when :belongs_to
126
+ params[:class_name] = self.class_name
127
+ when :has_many, :many_to_many
128
+ %w(class_name inverse_of order_by ui_enabled).each do |name|
129
+ params[name.to_sym] = self.send(name.to_sym)
130
+ end
131
+ end
132
+
133
+ params
134
+ end
135
+
136
+ # Instead of returning a simple hash, it returns a hash with name as the key and
137
+ # the remaining attributes as the value.
138
+ #
139
+ # @return [ Hash ] A hash of hash
140
+ #
141
+ def to_hash
142
+ hash = super.delete_if { |k, v| %w(name position).include?(k) }
143
+
144
+ # class_name is chosen over class_slug
145
+ if self.is_relationship?
146
+ hash['class_name'] ||= hash['class_slug']
147
+ hash.delete('class_slug')
148
+ end
149
+
150
+ # select options
151
+ if self.type == :select
152
+ hash['select_options'] = self.select_options_to_hash
153
+ end
154
+
155
+ { self.name => hash }
156
+ end
157
+
158
+ protected
159
+
160
+ # Clean up useless properties depending on its type
161
+ def sanitize
162
+ # ui_enabled only for the belongs_to, has_many and many_to_many types
163
+ self.ui_enabled = nil unless self.is_relationship?
164
+
165
+ # text_formatting only for the text type
166
+ self.text_formatting = nil unless self.type == :text
167
+ end
168
+
169
+ def select_options_to_hash
170
+ locales = self.select_options.map { |option| option.translated_in }.flatten.uniq
171
+ options = self.select_options.sort { |a, b| a.position <=> b.position }
172
+
173
+ if locales.size > 1
174
+ {}.tap do |by_locales|
175
+ locales.each do |locale|
176
+ options.each do |option|
177
+ (by_locales[locale.to_s] ||= []) << option.name_translations[locale]
178
+ end
179
+ end
180
+ end
181
+ else
182
+ options.map(&:name)
183
+ end
184
+ end
185
+
186
+ end
187
+
188
+ end
189
+ end
190
+ end
@@ -0,0 +1,29 @@
1
+ module Locomotive
2
+ module Mounter
3
+ module Models
4
+
5
+ class ContentSelectOption < Base
6
+
7
+ ## fields ##
8
+ field :name, localized: true
9
+ field :position, default: 0
10
+
11
+ ## methods ##
12
+
13
+ # Return the params used for the API.
14
+ #
15
+ # @param [ Hash ] options For now, none
16
+ #
17
+ # @return [ Hash ] The params
18
+ #
19
+ def to_params(options = nil)
20
+ { name: self.name_translations, position: self.position }.tap do |params|
21
+ params[:id] = self._id if self.persisted?
22
+ end
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,274 @@
1
+ module Locomotive
2
+ module Mounter
3
+ module Models
4
+
5
+ class ContentType < Base
6
+
7
+ ## fields ##
8
+ field :name
9
+ field :description
10
+ field :slug
11
+ field :label_field_name
12
+ field :group_by
13
+ field :order_by
14
+ field :order_direction
15
+ field :public_submission_enabled
16
+ field :public_submission_accounts
17
+ field :raw_item_template
18
+
19
+ field :fields, type: :array, class_name: 'Locomotive::Mounter::Models::ContentField'
20
+
21
+ field :entries, association: true
22
+
23
+ ## callbacks ##
24
+ set_callback :initialize, :after, :sanitize
25
+ set_callback :initialize, :after, :assign_mounting_point_to_fields
26
+
27
+ ## other accessors ##
28
+ attr_accessor :klass_name, :group_by_field_id
29
+
30
+ ## aliases ##
31
+ alias_method :group_by_field_name, :group_by
32
+ alias_method :group_by_field_name=, :group_by=
33
+
34
+ ## methods ##
35
+
36
+ # Return the label field (by the default the first field)
37
+ #
38
+ # @return [ Object ] The label field
39
+ #
40
+ def label_field
41
+ self.find_field(self.label_field_name) || self.fields.first
42
+ end
43
+
44
+ # Return the group_by field
45
+ #
46
+ # @return [ Object ] The group_by field
47
+ #
48
+ def group_by_field
49
+ self.find_field(self.group_by_field_name)
50
+ end
51
+
52
+ # Return the order_by field
53
+ #
54
+ # @return [ Object ] The order_by field
55
+ #
56
+ def order_by_field
57
+ self.find_field(self.order_by)
58
+ end
59
+
60
+ # Build a content entry and add it to the list of entries of the content type.
61
+ # The content type will be referenced into the newly built entry .
62
+ #
63
+ # @param [ Hash ] attributes Attributes of the new content entry
64
+ #
65
+ # @return [ Object ] The newly built content entry
66
+ #
67
+ def build_entry(attributes)
68
+ ContentEntry.new(content_type: self).tap do |entry|
69
+ # set the link to the mounting point
70
+ entry.mounting_point = self.mounting_point
71
+
72
+ # do not forget that we are manipulating dynamic fields
73
+ attributes.each do |k, v|
74
+ begin
75
+ field = self.find_field(k)
76
+
77
+ if field.nil? && v.is_a?(Hash) # not a dynamic field but localized (permalink ?)
78
+ entry.send(:"#{k}_translations=", v)
79
+ elsif field.nil?
80
+ entry.send(:"#{k}=", v)
81
+ else
82
+ entry.send(:"#{field.name}=", v)
83
+ end
84
+ rescue NoMethodError => e
85
+ Mounter.logger.error e.backtrace
86
+ raise FieldDoesNotExistException.new("The '#{self.slug}' content type does not have a field named '#{k}'.")
87
+ end
88
+ end
89
+
90
+ # force the slug to be defined from its label and in all the locales
91
+ entry.send :set_slug
92
+
93
+ (self.entries ||= []) << entry
94
+ end
95
+ end
96
+
97
+ # Tell if the content type owns a field which defines
98
+ # a relationship to another content type.
99
+ #
100
+ # @return [ Boolean ] True if a relationship field exists
101
+ #
102
+ def with_relationships?
103
+ self.fields.any? { |field| field.is_relationship? }
104
+ end
105
+
106
+ # Return the list of fields which do not describe
107
+ # a relationship.
108
+ #
109
+ # @return [ Array ] The list of fields.
110
+ #
111
+ def non_relationship_fields
112
+ self.fields.select { |field| !field.is_relationship? }
113
+ end
114
+
115
+ # Return the list of file fields.
116
+ #
117
+ # @return [ Array ] The list of file fields.
118
+ #
119
+ def file_fields
120
+ self.fields.select { |field| field.type == :file }
121
+ end
122
+
123
+ # Find a field by its name (string or symbol) or its id (API)
124
+ #
125
+ # @param [ String / Symbol] name_or_id Name or Id of the field
126
+ #
127
+ # @return [ Object ] The field if it exists or nil
128
+ #
129
+ def find_field(name_or_id)
130
+ self.fields.detect { |field| field.matches?(name_or_id) }
131
+ end
132
+
133
+ # Find a content entry by its ids (ie: _permalink or _label)
134
+ #
135
+ # @param [ String ] id A permalink or a label
136
+ #
137
+ # @return [ Object ] The content entry if it exists or nil
138
+ #
139
+ def find_entry(id)
140
+ (self.entries || []).detect { |entry| [entry._permalink, entry._label].include?(id) }
141
+ end
142
+
143
+ # Find all the entries whose their _permalink or _label is among the ids
144
+ # passed in parameter.
145
+ #
146
+ # @param [ Array ] ids List of permalinks or labels
147
+ #
148
+ # @return [ Array ] List of content entries or [] if none
149
+ #
150
+ def find_entries_among(ids)
151
+ (self.entries || []).find_all { |entry| [*ids].any? { |v| [entry._permalink, entry._label].include?(v) } }
152
+ end
153
+
154
+ # Find all the entries by a field and its value.
155
+ #
156
+ # @param [ String ] name Name of the field
157
+ # @param [ String / Array ] value The different values of the field to test
158
+ #
159
+ # @return [ Array ] List of content entries or [] if none
160
+ #
161
+ def find_entries_by(name, value)
162
+ values = [*value]
163
+ (self.entries || []).find_all do |entry|
164
+ raw_value = entry.send(:localized_dynamic_attribute_value, name)
165
+ values.include?(raw_value)
166
+ end
167
+ end
168
+
169
+ # Return the params used for the API.
170
+ # The options parameter can be used to get all the fields even
171
+ # the ones describing a relationship with another content type.
172
+ #
173
+ # @param [ Hash ] options Default values: { all_fields: false }
174
+ #
175
+ # @return [ Hash ] The params
176
+ #
177
+ def to_params(options = nil)
178
+ options = { all_fields: false }.merge(options || {})
179
+
180
+ params = self.filter_attributes %w(name slug description label_field_name group_by_field_name order_by order_direction public_submission_enabled raw_item_template)
181
+
182
+ # group by
183
+ params[:group_by_field_name] = self.group_by_field.name if self.group_by_field
184
+
185
+ # order by
186
+ params[:order_by] = '_position' if self.order_by == 'manually'
187
+
188
+ # fields
189
+ _fields = options[:all_fields] ? self.fields : self.non_relationship_fields
190
+ params[:entries_custom_fields] = _fields.map(&:to_params)
191
+
192
+ params
193
+ end
194
+
195
+ # Return a hash with sanitized attributes. It will be used to generate
196
+ # the corresponding yaml file.
197
+ #
198
+ # @return [ Hash ] A hash used by the to_yaml method
199
+ #
200
+ def to_hash
201
+ fields = %w(name slug description label_field_name order_by order_direction public_submission_enabled public_submission_accounts raw_item_template)
202
+
203
+ _attributes = self.attributes.delete_if { |k, v| !fields.include?(k.to_s) || v.blank? }.deep_stringify_keys
204
+
205
+ # group by
206
+ _attributes['group_by'] = self.group_by_field.name if self.group_by_field
207
+
208
+ # order by
209
+ _attributes['order_by'] = self.order_by_field.name if self.order_by_field
210
+ _attributes['order_by'] = 'manually' if self.order_by == '_position'
211
+
212
+ # custom fields
213
+ _attributes['fields'] = self.fields
214
+
215
+ _attributes
216
+ end
217
+
218
+ def to_s
219
+ self.name
220
+ end
221
+
222
+ protected
223
+
224
+ # Give an unique slug based on a label and within the scope of the content type.
225
+ #
226
+ # @param [ String ] label The label. If nil, we take the singularized version of the content type slug.
227
+ #
228
+ # @return [ String ] An unique slug
229
+ #
230
+ def label_to_slug(label = nil)
231
+ label ||= self.slug.singularize
232
+ base, index = label.parameterize('-'), 1
233
+ unique_slug = base
234
+
235
+ while self.find_entry(unique_slug)
236
+ unique_slug = "#{base}-#{index}"
237
+ index += 1
238
+ end
239
+
240
+ unique_slug
241
+ end
242
+
243
+ # Method used to clean up the content and its fields.
244
+ # Besides, it also sets the values defined by other attributes.
245
+ def sanitize
246
+ # if no label_field_name provided, take the first field
247
+ unless self.label_field_name
248
+ self.label_field_name = (self.fields || []).first.try(:name)
249
+ end
250
+
251
+ # define group_by_field from group_by_field_id
252
+ if self.group_by_field_name.blank? && self.group_by_field_id.present?
253
+ self.group_by_field_name = self.find_field(self.group_by_field_id)
254
+ end
255
+
256
+ # public_submission_accounts means public_submission_enabled set to true
257
+ self.public_submission_enabled = true if self.public_submission_accounts.is_a?(Array)
258
+ end
259
+
260
+ # Each field should have a reference to the mounting point
261
+ #
262
+ def assign_mounting_point_to_fields
263
+ return if self.fields.blank?
264
+
265
+ self.fields.each do |field|
266
+ field.mounting_point = self.mounting_point
267
+ end
268
+ end
269
+
270
+ end
271
+
272
+ end
273
+ end
274
+ end