Class: RestaurantTagCpt::Operations::Update

Inherits:
Trailblazer::Operation
  • Object
show all
Defined in:
app/concepts/restaurant_tag_cpt/operations/update.rb

Instance Method Summary collapse

Instance Method Details

#find_model!(options) ⇒ Object



13
14
15
# File 'app/concepts/restaurant_tag_cpt/operations/update.rb', line 13

def find_model!(options, *)
  options['model'] = ::RestaurantTagCpt::ModelDecorator.find_by(id: options[:id])
end

#map_attributes!(options) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/concepts/restaurant_tag_cpt/operations/update.rb', line 17

def map_attributes!(options, *)
  MyLocaleManager.available_locales.each do |locale|
    tag_value = options[::OpCons::CONTRACT_DEFAULT].send("tag_#{locale}").to_s.strip

    # Only update if tag_value is present
    # Don't set to nil for required locales (en, th, cn) to avoid validation errors
    if tag_value.present?
      title = "#{options[::OpCons::CONTRACT_DEFAULT].category}:#{tag_value}"
      options[::OpCons::MODEL].send(:"title_#{locale}=", title)
    elsif !RestaurantTag::REQUIRED_LOCALES.include?(locale)
      # For optional locales, allow setting to nil
      options[::OpCons::MODEL].send(:"title_#{locale}=", nil)
    end
    # For required locales (en, th, cn), preserve existing value if tag_value is blank
  end
end

#update_restaurants!(options) ⇒ Object



34
35
36
37
# File 'app/concepts/restaurant_tag_cpt/operations/update.rb', line 34

def update_restaurants!(options, *)
  Restaurant.joins(:primary_tags).where(primary_tags: { restaurant_tag_id: options['result.contract.default'].model.id }).
    update_all(updated_at: Time.zone.now)
end