class Locomotive::TscLiquidExtensions::Tags::IfUpdate

Public Instance Methods

display(*options) { |false| ... } click to toggle source
# File lib/tsc_liquid_extensions/tags/if_update.rb, line 11
def display(*options, &block)
  drop, attributes = self.extract_drop_and_attributes(options)
  model = drop.instance_variable_get(:@_source)

  return yield(false) if model.nil?

  if wagon
    model.attributes = model.attributes.merge( attributes)
    # save the model in the engine or just process the validation if in Wagon
  else
    model.views = model.views + 1
  end

  persisted = model.valid?

  # log the last statement
  self.log(model, persisted)

  yield(persisted)
end

Protected Instance Methods

extract_drop_and_attributes(options) click to toggle source
# File lib/tsc_liquid_extensions/tags/if_update.rb, line 34
def extract_drop_and_attributes(options)
  raise Liquid::Error.new("[update] wrong number of parameters (2 are required instead of #{options.size})") if options.size != 2

  [options.first, options.last].tap do |drop, attributes|
    if attributes.is_a?(Hash)
      attributes.each do |k, v|
        _source = v.instance_variable_get(:@_source)

        attributes[k] = _source if _source
      end

      attributes.delete_if { |k, _| %w(site site_id content_type content_type_id).include?(k.to_s) }
    else
      raise Liquid::Error.new('[update] wrong attributes')
    end
  end
end
log(model, persisted) click to toggle source
# File lib/tsc_liquid_extensions/tags/if_update.rb, line 52
def log(model, persisted)
  message = persisted ? ["Model updated !"] : ["Model not updated :-("]
  message << "  attributes: #{model.to_s}"

  current_context.registers[:logger].info message.join("\n") + "\n\n"
end