Sha256: cfacbc15fbcd97e13f0266efdff55ca88db2b48fea8c828886241ca8db63040b

Contents?: true

Size: 1.4 KB

Versions: 25

Compression:

Stored size: 1.4 KB

Contents

class Backend::TranslationWithSeoForm < Udongo::Form
  attr_reader :model, :translation, :seo
  delegate :id, to: :model

  attribute :seo_title, String
  attribute :seo_keywords, String
  attribute :seo_description, String
  attribute :seo_custom, String
  attribute :seo_slug, String

  validates :seo_slug, :seo_title, :seo_description, presence: true

  def initialize(model, translation, seo)
    @model = model
    @translation = translation
    @seo = seo

    non_seo_attributes.each do |f|
      self.send "#{f}=", @translation.send(f)
    end

    seo_attributes.each do |f|
      self.send("seo_#{f}=", @seo.send(f))
    end
  end

  def persisted?
    true
  end

  def save(params)
    non_seo_attributes.each do |f|
      self.send "#{f}=", params[f.to_sym]
    end

    seo_attributes.each do |f|
      self.send("seo_#{f}=", params["seo_#{f}".to_sym])
    end

    if valid?
      save_object
      true
    else
      false
    end
  end

  def seo_attributes
    %w(title keywords description custom slug)
  end

  def non_seo_attributes
    attributes.keys.select { |k| !k.to_s.starts_with?('seo_') }
  end

  private

  def save_object
    non_seo_attributes.each do |f|
      @translation.send "#{f}=", self.send(f)
    end

    seo_attributes.each do |f|
      @seo.send("#{f}=", send("seo_#{f}"))
    end

    # Saves the model, translation and SEO info all at once.
    @seo.save!
    @model.save!
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
udongo-7.9.0 app/forms/backend/translation_with_seo_form.rb
udongo-7.8.1 app/forms/backend/translation_with_seo_form.rb
udongo-7.8.0 app/forms/backend/translation_with_seo_form.rb
udongo-7.7.2 app/forms/backend/translation_with_seo_form.rb
udongo-7.7.1 app/forms/backend/translation_with_seo_form.rb
udongo-7.7.0 app/forms/backend/translation_with_seo_form.rb
udongo-7.6.2 app/forms/backend/translation_with_seo_form.rb
udongo-7.6.1 app/forms/backend/translation_with_seo_form.rb
udongo-7.6.0 app/forms/backend/translation_with_seo_form.rb
udongo-7.5.1 app/forms/backend/translation_with_seo_form.rb
udongo-7.5.0 app/forms/backend/translation_with_seo_form.rb
udongo-7.4.2 app/forms/backend/translation_with_seo_form.rb
udongo-7.4.1 app/forms/backend/translation_with_seo_form.rb
udongo-7.4.0 app/forms/backend/translation_with_seo_form.rb
udongo-7.3.5 app/forms/backend/translation_with_seo_form.rb
udongo-7.3.4 app/forms/backend/translation_with_seo_form.rb
udongo-7.3.3 app/forms/backend/translation_with_seo_form.rb
udongo-7.3.2 app/forms/backend/translation_with_seo_form.rb
udongo-7.3.1 app/forms/backend/translation_with_seo_form.rb
udongo-7.2.1 app/forms/backend/translation_with_seo_form.rb