Class: Lit::LocalizationKey

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/lit/localization_key.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#interpolated_keyObject

Returns the value of attribute interpolated_key.



3
4
5
# File 'app/models/lit/localization_key.rb', line 3

def interpolated_key
  @interpolated_key
end

Class Method Details

.default_search_optionsObject

it can be overridden in parent application, for example: => “created_at desc”



60
61
62
# File 'app/models/lit/localization_key.rb', line 60

def self.default_search_options
  {}
end

.order_optionsObject



55
56
57
# File 'app/models/lit/localization_key.rb', line 55

def self.order_options
  ["localization_key asc", "localization_key desc", "created_at asc", "created_at desc"]
end

.search(options = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/lit/localization_key.rb', line 64

def self.search(options={})
  options = options.reverse_merge(default_search_options)
  s = self
  if options[:order] && order_options.include?(options[:order])
    column, order = options[:order].split(" ")
    s = s.order("#{Lit::LocalizationKey.quoted_table_name}.#{connection.quote_column_name(column)} #{order}" )
  else
    s = s.ordered
  end
  localization_key_col = Lit::LocalizationKey.arel_table[:localization_key]
  default_value_col = Lit::Localization.arel_table[:default_value]
  translated_value_col = Lit::Localization.arel_table[:translated_value]
  if options[:key_prefix].present?
    q = "#{options[:key_prefix]}%"
    s = s.where(localization_key_col.matches(q))
  end
  if options[:key].present?
    q = "%#{options[:key]}%"
    cond = localization_key_col.matches(q).or(
        default_value_col.matches(q).or(
            translated_value_col.matches(q)
        )
    )
    s = s.joins([:localizations]).where(cond)
  end
  if not options[:include_completed].to_i==1
    s = s.not_completed
  end
  s
end

Instance Method Details

#clone_localizationsObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/lit/localization_key.rb', line 29

def clone_localizations
  new_created = false
  Lit::Locale.find_each do |locale|
    self.localizations.where(:locale_id=>locale.id).first_or_create do |l|
      l.default_value = interpolated_key
      new_created = true
    end
  end
  if new_created
    Lit::LocalizationKey.update_all ['is_completed=?', false], ['id=? and is_completed=?', self.id, false]
  end
end

#completedObject

SCOPES



6
# File 'app/models/lit/localization_key.rb', line 6

scope :completed, proc{ where(:is_completed=>true) }

#localization_keyObject

ACCESSIBLE



16
17
18
# File 'app/models/lit/localization_key.rb', line 16

validates :localization_key,
:presence=>true,
:uniqueness=>{:if=>:localization_key_changed?}

#localizationsObject

ASSOCIATIONS



13
# File 'app/models/lit/localization_key.rb', line 13

has_many :localizations, :dependent=>:destroy

#mark_all_completed!Object



50
51
52
53
# File 'app/models/lit/localization_key.rb', line 50

def mark_all_completed!
  self.localizations.update_all(['is_changed=?', true])
  mark_completed!
end

#mark_completedObject



42
43
44
# File 'app/models/lit/localization_key.rb', line 42

def mark_completed
  self.is_completed = self.localizations.changed.count(:id) == self.localizations.count
end

#mark_completed!Object



46
47
48
# File 'app/models/lit/localization_key.rb', line 46

def mark_completed!
  self.save if self.mark_completed
end

#to_sObject



25
26
27
# File 'app/models/lit/localization_key.rb', line 25

def to_s
  self.localization_key
end