Class: Locomotive::ContentEntryService

Inherits:
Struct
  • Object
show all
Includes:
Locomotive::Concerns::ActivityService
Defined in:
app/services/locomotive/content_entry_service.rb

Direct Known Subclasses

Steam::APIContentEntryService

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Locomotive::Concerns::ActivityService

#track_activity, #without_tracking_activity

Instance Attribute Details

#accountObject

Returns the value of attribute account

Returns:

  • (Object)

    the current value of account


2
3
4
# File 'app/services/locomotive/content_entry_service.rb', line 2

def 
  @account
end

#content_typeObject

Returns the value of attribute content_type

Returns:

  • (Object)

    the current value of content_type


2
3
4
# File 'app/services/locomotive/content_entry_service.rb', line 2

def content_type
  @content_type
end

#localeObject

Returns the value of attribute locale

Returns:

  • (Object)

    the current value of locale


2
3
4
# File 'app/services/locomotive/content_entry_service.rb', line 2

def locale
  @locale
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

Parameters:

  • options (Hash) (defaults to: {})

    The options for the pagination and the filtering

Returns:

  • (Object)

    a paginated list of content entries


22
23
24
25
26
# File 'app/services/locomotive/content_entry_service.rb', line 22

def all(options = {})
  _options = prepare_options_for_all(options)

  content_type.ordered_entries(_options)
end

#bulk_destroy(ids) ⇒ Object

Destroy all the entries described by their id


131
132
133
134
135
136
137
138
139
# File 'app/services/locomotive/content_entry_service.rb', line 131

def bulk_destroy(ids)
  content_type.entries.where(:_id.in => ids).map do |entry|
    entry.destroy
    entry._label
  end.tap do |labels|
    track_activity 'content_entry.destroyed_bulk',
      parameters: activity_parameters.merge(labels: labels)
  end
end

#create(attributes) ⇒ Object

Create a content entry from the attributes passed in parameter. It sets the created_by column with the current account.

Parameters:

  • attributes (Hash)

    The attributes of new content entry.

Returns:

  • (Object)

    An instance of the content entry.


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 =  if 

    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_allObject

Destroy all the entries of a content type. Runs each entry's destroy callbacks.


144
145
146
# File 'app/services/locomotive/content_entry_service.rb', line 144

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)


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/services/locomotive/content_entry_service.rb', line 151

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_attributesArray

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)

Returns:

  • (Array)

    List of permitted attributes


188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'app/services/locomotive/content_entry_service.rb', line 188

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 |name, inverse_of|
    referenced["#{name}_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.

Parameters:

  • attributes (Hash)

    The attributes of new content entry.

  • options (Hash) (defaults to: {})

    For now, only store the ip address of the person who submitted the content entry + other accounts

Returns:

  • (Object)

    An instance of the content entry.


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, options = {})
  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 + potential external accounts described by their emails
      send_notifications(entry, options[:emails])

      track_activity 'content_entry.created_public', locale: locale, parameters: activity_parameters(entry)
    end
  end
end

#send_notifications(entry, emails = nil) ⇒ Object


169
170
171
172
173
174
175
176
177
178
# File 'app/services/locomotive/content_entry_service.rb', line 169

def send_notifications(entry, emails = nil)
  return unless self.content_type.public_submission_enabled?

  Locomotive::Account.any_of(
    { :_id.in   => self.content_type.public_submission_accounts || [] },
    { :email.in => emails || [] }
  ).each do ||
    Locomotive::Notifications.new_content_entry(, entry).deliver
  end
end

#siteObject


209
210
211
# File 'app/services/locomotive/content_entry_service.rb', line 209

def site
  self.content_type.site
end

#sort(ids) ⇒ Object

Sort the entries of a content type.

Parameters:

  • ids (Array)

    Ordered list of content entry ids.


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.

Parameters:

  • entry (Object)

    The content entry to update.

  • attributes (Hash)

    The attributes of new content entry.

Returns:

  • (Object)

    The instance of the content entry.


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 = 

    if entry.save
      track_activity 'content_entry.updated', locale: locale, 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