Method: Tml::Tokens::Data#token_values_from_array

Defined in:
lib/tml/tokens/data.rb

#token_values_from_array(params, language, options) ⇒ Object

tr(“Hello user_list!”, “”, => [[user1, user2, user3], :name]}

first element is an array, the rest of the elements are similar to the regular tokens lambda, symbol, string, with parameters that follow

if you want to pass options, then make the second parameter an array as well

tr(“users joined the site”, => [[user1, user2, user3], :name])

tr(“users joined the site”, => [[user1, user2, user3], lambda{|user| user.name]})

tr(“users joined the site”, => [[user1, user2, user3], {:attribute => :name)

tr(“users joined the site”, => [[user1, user2, user3], {:attribute => :name, :value => “<strong>{$0</strong>”})

tr(“users joined the site”, => [[user1, user2, user3], “<strong>{$0</strong>”)

tr(“users joined the site”, => [[user1, user2, user3], :name, {

:limit => 4,
:separator => ', ',
:joiner => 'and',
:remainder => lambda{|elements| tr("#{count||other", :count => elements.size)},
:translate => false,
:expandable => true,
:collapsable => true

})



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/tml/tokens/data.rb', line 159

def token_values_from_array(params, language, options)
  list_options = {
   :description => "List joiner",
   :limit => 4,
   :separator => ", ",
   :joiner => 'and',
   :less => '{laquo} less',
   :expandable => true,
   :collapsable => true
  }

  objects = params[0]
  method = params[1]
  list_options.merge!(params[2]) if params.size > 2
  list_options[:expandable] = false if options[:skip_decorations]

  values = objects.collect do |obj|
    if method.is_a?(String)
      element = method.gsub("{$0}", sanitize(obj.to_s, obj, language, options.merge(:safe => false)))
    elsif method.is_a?(Symbol)
      if obj.is_a?(Hash)
        value = Tml::Utils.hash_value(obj, method)
      else
        value = obj.send(method)
      end
      element = sanitize(value, obj, language, options.merge(:safe => false))
    elsif method.is_a?(Hash)
      attr = Tml::Utils.hash_value(method, :attribute) || Tml::Utils.hash_value(method, :property)
      if obj.is_a?(Hash)
        value = Tml::Utils.hash_value(obj, attr)
      else
        value = obj.send(method)
      end

      hash_value = Tml::Utils.hash_value(method, :value)
      if hash_value
        element = hash_value.gsub("{$0}", sanitize(value, obj, language, options.merge(:safe => false)))
      else
        element = sanitize(value, obj, language, options.merge(:safe => false))
      end
    elsif method.is_a?(Proc)
      element = sanitize(method.call(obj), obj, language, options.merge(:safe => true))
    end

    Tml::Decorators::Base.decorator.decorate_element(self, element, options)
  end

  return values.first if objects.size == 1
  return values.join(list_options[:separator]) if list_options[:joiner].nil? || list_options[:joiner] == ""

  joiner = language.translate(list_options[:joiner], list_options[:description], {}, options)
  if values.size <= list_options[:limit]
    return "#{values[0..-2].join(list_options[:separator])} #{joiner} #{values.last}"
  end

  display_ary = values[0..(list_options[:limit]-1)]
  remaining_ary = values[list_options[:limit]..-1]
  result = "#{display_ary.join(list_options[:separator])}"

  unless list_options[:expandable]
    result << " " << joiner << " "
    if list_options[:remainder] and list_options[:remainder].is_a?(Proc)
      result << list_options[:remainder].call(remaining_ary)
    else
      result << language.translate("{count||other}", list_options[:description], {:count => remaining_ary.size}, options)
    end
    return result
  end

  uniq_id = Tml::TranslationKey.generate_key(label, values.join(","))
  result << "<span id=\"tml_other_link_#{uniq_id}\"> #{joiner} "

  result << "<a href='#' onClick=\"document.getElementById('tml_other_link_#{uniq_id}').style.display='none'; document.getElementById('tml_other_elements_#{uniq_id}').style.display='inline'; return false;\">"
  if list_options[:remainder] and list_options[:remainder].is_a?(Proc)
    result << list_options[:remainder].call(remaining_ary)
  else
    result << language.translate("{count||other}", list_options[:description], {:count => remaining_ary.size}, options)
  end
  result << "</a></span>"

  result << "<span id=\"tml_other_elements_#{uniq_id}\" style='display:none'>"
  result << list_options[:separator] << " "
  result << remaining_ary[0..-2].join(list_options[:separator])
  result << " #{joiner} "
  result << remaining_ary.last

  if list_options[:collapsable]
    result << "<a href='#' style='font-size:smaller;white-space:nowrap' onClick=\"document.getElementById('tml_other_link_#{uniq_id}').style.display='inline'; document.getElementById('tml_other_elements_#{uniq_id}').style.display='none'; return false;\"> "
    result << language.translate(list_options[:less], list_options[:description], {}, options)
    result << "</a>"
  end

  result << "</span>"
end