Class: UniqueKey

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Admin::UniqueKey
Defined in:
app/models/unique_key.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_localized_viewable!(unique_key) ⇒ Object



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

def create_localized_viewable!(unique_key)
  viewable = nil
  I18n.available_locales.each do |locale|
    attributes = unique_key.merge(locale: locale, viewable: unique_key[:viewable_type].constantize.new)
    new_record = find_or_create_by! attributes
    if I18n.locale == locale
      viewable = new_record.viewable
    end
  end
  viewable.try(:uuid) # forces uuid creation
  viewable
end

.find_or_create_viewable!(unique_key) ⇒ Object



22
23
24
25
26
27
# File 'app/models/unique_key.rb', line 22

def find_or_create_viewable!(unique_key)
  unless (viewable = find_viewable(unique_key))
    viewable = create_localized_viewable!(unique_key)
  end
  viewable
end

.find_viewable(unique_key) ⇒ Object



18
19
20
# File 'app/models/unique_key.rb', line 18

def find_viewable(unique_key)
  (unique_key = find_by(unique_key)) ? unique_key.viewable : nil
end

Instance Method Details

#list(locale = nil) ⇒ Object



43
44
45
46
47
# File 'app/models/unique_key.rb', line 43

def list(locale = nil)
  self.class
    .where(viewable_type: viewable_type, view_path: view_path, name: name)
    .where(locale: locale || self.locale)
end

#other_locales(position = nil) ⇒ Object



49
50
51
52
53
54
# File 'app/models/unique_key.rb', line 49

def other_locales(position = nil)
  self.class
    .where(viewable_type: viewable_type, view_path: view_path, name: name)
    .where(position: position || self.position)
    .where.not(locale: locale)
end

#with_buffered_position(new_position) ⇒ Object



56
57
58
59
60
61
# File 'app/models/unique_key.rb', line 56

def with_buffered_position(new_position)
  update_column(:position, 0)
  yield
  update_column(:position, new_position)
  viewable.touch
end