Class: Locomotive::ContentEntryService
- Inherits:
-
Struct
- Object
- Struct
- Locomotive::ContentEntryService
- Includes:
- Locomotive::Concerns::ActivityService
- Defined in:
- app/services/locomotive/content_entry_service.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#account ⇒ Object
Returns the value of attribute account.
-
#content_type ⇒ Object
Returns the value of attribute content_type.
Instance Method Summary collapse
-
#all(options = {}) ⇒ Object
List all the entries of a content type.
-
#create(attributes) ⇒ Object
Create a content entry from the attributes passed in parameter.
- #create!(attributes) ⇒ Object
- #destroy(entry) ⇒ Object
-
#destroy_all ⇒ Object
Destroy all the entries of a content type.
-
#localize(new_locales, previous_default_locale) ⇒ Object
Make sure the content entries has a non-blank slug in the new locales.
-
#permitted_attributes ⇒ Array
Give the list of permitted attributes for a content entry.
-
#public_create(attributes, options = {}) ⇒ Object
Create a content entry from the attributes passed in parameter.
- #send_notifications(entry) ⇒ Object
- #site ⇒ Object
-
#sort(ids) ⇒ Object
Sort the entries of a content type.
-
#update(entry, attributes) ⇒ Object
Update a content entry from the attributes passed in parameter.
- #update!(entry, attributes) ⇒ Object
Methods included from Locomotive::Concerns::ActivityService
#track_activity, #without_tracking_activity
Instance Attribute Details
#account ⇒ Object
Returns the value of attribute account
2 3 4 |
# File 'app/services/locomotive/content_entry_service.rb', line 2 def account @account end |
#content_type ⇒ Object
Returns the value of attribute content_type
2 3 4 |
# File 'app/services/locomotive/content_entry_service.rb', line 2 def content_type @content_type end |
Instance Method Details
#all(options = {}) ⇒ Object
List all the entries of a content type.
If the content type allows the pagination, in other words, if the entries are not ordered by the position column), then this method will return a paginated list of entries.
This list can also be filtered by the _label attribute, by setting the “q” key in the options.
For a more powerful search, you can use the “where” key which accepts a JSON string or a Hash.
The no_pagination option is used to skip the pagination of the content entries
22 23 24 25 26 |
# File 'app/services/locomotive/content_entry_service.rb', line 22 def all( = {}) = () content_type.ordered_entries() end |
#create(attributes) ⇒ Object
Create a content entry from the attributes passed in parameter. It sets the created_by column with the current account.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/services/locomotive/content_entry_service.rb', line 49 def create(attributes) sanitize_attributes!(attributes) content_type.entries.build(attributes).tap do |entry| entry.created_by = account if account if entry.save track_activity 'content_entry.created', parameters: activity_parameters(entry) end end end |
#create!(attributes) ⇒ Object
61 62 63 64 65 66 67 |
# File 'app/services/locomotive/content_entry_service.rb', line 61 def create!(attributes) if (entry = create(attributes)).errors.empty? entry else entry.fail_due_to_validation! end end |
#destroy(entry) ⇒ Object
124 125 126 127 128 |
# File 'app/services/locomotive/content_entry_service.rb', line 124 def destroy(entry) entry.destroy.tap do track_activity 'content_entry.destroyed', parameters: activity_parameters(entry) end end |
#destroy_all ⇒ Object
Destroy all the entries of a content type. Runs each entry’s destroy callbacks.
155 156 157 |
# File 'app/services/locomotive/content_entry_service.rb', line 155 def destroy_all content_type.entries.destroy_all end |
#localize(new_locales, previous_default_locale) ⇒ Object
Make sure the content entries has a non-blank slug in the new locales. We take the slug in the default locale or the previous default locale (if provided) in the other locales.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'app/services/locomotive/content_entry_service.rb', line 134 def localize(new_locales, previous_default_locale) default_locale = previous_default_locale || content_type.site.default_locale content_type.entries.each_by(50) do |entry| slug = entry._slug_translations[default_locale] new_locales.each do |locale| next if locale == default_locale ::Mongoid::Fields::I18n.with_locale(locale) do entry._slug ||= slug end end entry.save if entry.changed? end end |
#permitted_attributes ⇒ Array
Give the list of permitted attributes for a content entry. It includes:
-
the default ones (_slug, seo, …etc)
-
the dynamic simple attributes (strings, texts, dates, belongs_to, …etc)
-
the relationships (has_many + many_to_many)
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'app/services/locomotive/content_entry_service.rb', line 179 def permitted_attributes # needed to get the custom fields _entry = content_type.entries.build default = %w(_slug _position _visible seo_title meta_keywords meta_description) dynamic = _entry.custom_fields_safe_setters referenced = {} # has_many has_many = _entry.has_many_custom_fields.each do |n, inverse_of| referenced["#{n}_attributes"] = [:_id, :_destroy, :"position_in_#{inverse_of}"] end # many_to_many many_to_many = _entry.many_to_many_custom_fields.each do |(_, s)| referenced[s] = [] end (default + dynamic + [referenced.empty? ? nil : referenced]).compact end |
#public_create(attributes, options = {}) ⇒ Object
Create a content entry from the attributes passed in parameter. It does not set the created_by column since it’s called from the public side of the site with no logged in account. The attributes are filtered through the Grape::Entity / Form. A notification email is sent to the selected members of the site.
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'app/services/locomotive/content_entry_service.rb', line 80 def public_create(attributes, = {}) form = Locomotive::API::Forms::ContentEntryForm.new(self.content_type, attributes) without_tracking_activity { create(form.serializable_hash) }.tap do |entry| if entry.errors.empty? # send an email to selected local accounts send_notifications(entry) track_activity 'content_entry.created_public', parameters: activity_parameters(entry) end end end |
#send_notifications(entry) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 |
# File 'app/services/locomotive/content_entry_service.rb', line 159 def send_notifications(entry) return unless self.content_type.public_submission_enabled? account_ids = (self.content_type.public_submission_accounts || []).map(&:to_s) self.content_type.site.accounts.each do |account| next unless account_ids.include?(account._id.to_s) Locomotive::Notifications.new_content_entry(account, entry).deliver end end |
#site ⇒ Object
200 201 202 |
# File 'app/services/locomotive/content_entry_service.rb', line 200 def site self.content_type.site end |
#sort(ids) ⇒ Object
Sort the entries of a content type.
32 33 34 35 36 37 38 39 40 |
# File 'app/services/locomotive/content_entry_service.rb', line 32 def sort(ids) klass = self.content_type.klass_with_custom_fields(:entries) klass.sort_entries!(ids, self.content_type.sortable_column) # we need to clear out the cache content_type.site.touch(:content_version) track_activity 'content_entry.sorted', parameters: activity_parameters end |
#update(entry, attributes) ⇒ Object
Update a content entry from the attributes passed in parameter. It sets the updated_by column with the current account.
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'app/services/locomotive/content_entry_service.rb', line 101 def update(entry, attributes) sanitize_attributes!(attributes) entry.tap do |entry| entry.attributes = attributes entry.updated_by = account if entry.save track_activity 'content_entry.updated', parameters: activity_parameters(entry) end end end |
#update!(entry, attributes) ⇒ Object
114 115 116 117 118 119 120 121 122 |
# File 'app/services/locomotive/content_entry_service.rb', line 114 def update!(entry, attributes) update(entry, attributes) if entry.errors.empty? entry else entry.fail_due_to_validation! end end |