Class: Locomotive::Steam::ContentEntryRepository::Conditions

Inherits:
Object
  • Object
show all
Defined in:
lib/locomotive/steam/repositories/content_entry_repository.rb

Instance Method Summary collapse

Constructor Details

#initialize(conditions = {}, fields, target_repository) ⇒ Conditions

Returns a new instance of Conditions.



212
213
214
215
216
217
218
219
220
221
222
# File 'lib/locomotive/steam/repositories/content_entry_repository.rb', line 212

def initialize(conditions = {}, fields, target_repository)
  @conditions = conditions.try(:with_indifferent_access) || {}
  @fields, @operators = fields, {}
  @target_repository = target_repository
  @locale = target_repository.locale

  @conditions.each do |name, value|
    _name, operator = name.to_s.split('.')
    @operators[_name] = operator if operator
  end
end

Instance Method Details

#prepareObject



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/locomotive/steam/repositories/content_entry_repository.rb', line 224

def prepare
  # _id (primary key)
  _prepare([Locomotive::Steam::ContentTypeField.new(name: '_id')]) do |_, value|
    @target_repository.adapter.make_id(value)
  end

  # select
  _prepare(@fields.selects) do |field, value|
    # FIXME: [only in Wagon], if the user changes the locale, since all content is stored in memory,
    # we have to change the locale in the repository used to fetch the select options.
    field.select_options.locale = @locale

    field.select_options.by_name(value).try(:_id)
  end

  # date
  _prepare(@fields.dates_and_date_times) { |field, value| value_to_date(value, field.type) }

  # belongs_to
  _prepare(@fields.belongs_to) { |field, value| value_to_id(value, field.target_id) }

  # many_to_many
  _prepare(@fields.many_to_many) { |field, value| values_to_ids(value, field.target_id) }

  @conditions
end