Module: Ixtlan::Models::I18nText

Included in:
Word
Defined in:
lib/ixtlan/models/i18n_text.rb

Constant Summary collapse

TEXT_LOCALE =
Object.full_const_get(Models::LOCALE)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(model) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ixtlan/models/i18n_text.rb', line 83

def self.included(model)
  model.send(:include, DataMapper::Resource)
  model.send(:include, UpdateChildren)

  model.property :id, ::DataMapper::Types::Serial

  model.property :code, String, :required => true, :length => 64

  model.property :text, String, :required => true, :length => 256

  model.property :version, Integer, :required => false

  model.property :current, ::DataMapper::Types::Boolean, :required => true, :auto_validation => false

  model.property :previous, ::DataMapper::Types::Boolean, :required => true, :auto_validation => false

  model.property :updated_at, DateTime, :required => true, :auto_validation => false

  model.belongs_to :updated_by, :model => Models::USER

  model.property :approved_at, DateTime, :required => false

  model.belongs_to :approved_by, :model => Models::USER, :required => false

  model.belongs_to :locale, :model => Models::LOCALE

  model.validates_with_method :invariant

  model.validates_with_method :cascade
  model.before :save do
    if(new? or attribute_get(:version).nil?)
      attribute_set(:version, nil)
      attribute_set(:current, false)
      attribute_set(:previous, false)
      attribute_set(:approved_at, nil)
      approved_by = nil
    end
  end

  model.class_eval <<-EOS, __FILE__, __LINE__
  def self.default_storage_name
    "Text"
  end

  def self.latest_approved(args = {})
    args[:current] = true
    all(args)
  end

  def self.second_latest_approved(args = {})
    args[:previous] = true
    all(args)
  end

  def self.approved(args = {})
    args[:version.not] = nil
    all(args)
  end

  def self.not_approved(args = {})
    args[:version] = nil
    all(args)
  end
EOS
end

Instance Method Details

#approve(params = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ixtlan/models/i18n_text.rb', line 58

def approve(params = {})
  latest = self.class.latest_approved(:code => attribute_get(:code),
                                      :locale => locale).first
  previous = self.class.second_latest_approved(:code => attribute_get(:code),
                                               :locale => locale).first
  params[:version] = latest.nil? ? 1 : latest.version + 1
  params[:current] = true
  # TODO approved is not need since after approval the resource is inmutable !!!
  params[:approved_at] = attribute_get(:updated_at)
  params[:approved_by] = params[:current_user] || current_user

  prev = (previous.nil? ? true : previous.update(:previous => false,
                                              :current_user => params[:current_user] || current_user))
  lat = (latest.nil? ? true : latest.update(:current => false,
                                          :previous => true,
                                          :current_user => params[:current_user] || current_user))
  upd = update(params)

  upd && lat && prev
end

#approved?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/ixtlan/models/i18n_text.rb', line 79

def approved?
  !attribute_get(:version).nil?
end

#cascadeObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ixtlan/models/i18n_text.rb', line 41

def cascade
  size =
    case locale.code.size
    when 2
      self.model.latest_approved(:code => code, :locale => TEXT_LOCALE.default).size
    when 5
      self.model.latest_approved(:code => code, :locale => TEXT_LOCALE.first(:code => locale.code[0,1])).size
    else
      1
    end
  if(size == 0)
    [false, "parent text for '#{code}' does not exists"]
  else
    [true]
  end
end

#invariantObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ixtlan/models/i18n_text.rb', line 10

def invariant
  no_version = original_attributes[:version].nil? && attribute_get(:version).nil?
  if no_version
    if (!attribute_get(:version).nil? && attribute_dirty?(:version) && attribute_dirty?(:text))
      [false, 'can not approve and change text at the same time']
    elsif new?
      [true]
    elsif attribute_dirty?(:code)
      [false, 'code is invariant']
    else
      [true]
    end
  else
    if (!attribute_get(:version).nil? && attribute_dirty?(:version))
      if (attribute_dirty?(:text))
        [false, 'can not approve and change text at the same time']
      elsif !original_attributes[properties[:version]].nil?
        [false, 'can not change version']
      else
        [true]
      end
    elsif attribute_dirty?(:code)
      [false, 'can not change code']
    elsif attribute_dirty?(:text)
      [false, 'can not change text']
    else
      [true]
    end
  end
end