Sha256: 67f0fa08651ef7887a6179c1d21c333098f3a0cdaa981be68065fafbba40f4ad

Contents?: true

Size: 1.79 KB

Versions: 3

Compression:

Stored size: 1.79 KB

Contents

module Lit
  class Localization < ActiveRecord::Base
    serialize :translated_value
    serialize :default_value

    ## SCOPES
    scope :changed, proc{ where(:is_changed=>true) }
    # @HACK: dirty, find a way to round date to full second
    scope :after, proc{|dt| where('updated_at >= ?', dt+1.second) }

    ## ASSOCIATIONS
    belongs_to :locale
    belongs_to :localization_key
    has_many :localization_versions, dependent: :destroy
    has_many :versions, class_name: '::Lit::LocalizationVersion'

    ## VALIDATIONS
    validates :locale_id,
              :presence=>true

    unless defined?(::ActionController::StrongParameters)
      ## ACCESSIBLE
      attr_accessible :translated_value, :locale_id
    end

    ## BEFORE & AFTER
    before_update :update_is_changed
    before_update :create_version
    after_update :mark_localization_key_completed

    def to_s
      self.get_value
    end

    def full_key
      "#{self.locale.locale}.#{self.localization_key.localization_key}"
    end

    def get_value
      (is_changed? && (not self.translated_value.nil?)) ? self.translated_value : self.default_value
    end

    def value
      get_value
    end

    def localization_key_str
      self.localization_key.localization_key
    end

    def locale_str
      self.locale.locale
    end

    def last_change
      self.updated_at.to_s(:db)
    end

    private
      def update_is_changed
        self.is_changed = true unless is_changed?
      end

      def mark_localization_key_completed
        self.localization_key.mark_completed!
      end

      def create_version
        if self.translated_value.present? and (not self.translated_value.nil?)
          l = self.localization_versions.new
          l.translated_value = self.translated_value_was || self.default_value
        end
      end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lit-0.2.1 app/models/lit/localization.rb
lit-0.2.0 app/models/lit/localization.rb
lit-0.1.0 app/models/lit/localization.rb