Module: Storefront::Helpers::ContentHelper

Included in:
Components::Base, Components::Form::Base
Defined in:
lib/storefront/helpers/content_helper.rb

Constant Summary

SCOPES_WITH_NAMESPACE_AND_NESTED_MODEL =
[
  '%{namespace}.%{model}.%{nested_model}.%{action}.%{attribute}',
  '%{model}.%{nested_model}.%{action}.%{attribute}',
  '%{namespace}.%{model}.%{action}.%{attribute}',
  '%{model}.%{action}.%{attribute}',
  '%{namespace}.%{model}.%{nested_model}.%{attribute}',
  '%{model}.%{nested_model}.%{attribute}',
  '%{namespace}.%{model}.%{attribute}',
  '%{model}.%{attribute}',
  '%{namespace}.%{nested_model}.%{attribute}',
  '%{nested_model}.%{attribute}',
  '%{namespace}.%{attribute}',
  '%{attribute}'
]
SCOPES_WITH_NESTED_MODEL =
[
  '%{model}.%{nested_model}.%{action}.%{attribute}',
  '%{model}.%{action}.%{attribute}',
  '%{model}.%{nested_model}.%{attribute}',
  '%{model}.%{attribute}',
  '%{nested_model}.%{attribute}',
  '%{attribute}'
]
SCOPES_WITH_NAMESPACE =
[
  '%{namespace}.%{model}.%{action}.%{attribute}',
  '%{namespace}.%{model}.%{attribute}',
  '%{model}.%{action}.%{attribute}',
  '%{model}.%{attribute}',
  '%{namespace}.%{attribute}',
  '%{attribute}'
]
SCOPES =
[
  '%{model}.%{action}.%{attribute}',
  '%{model}.%{attribute}',
  '%{attribute}'
]

Instance Method Summary (collapse)

Instance Method Details

- (Object) encoded_contents(path)



191
192
193
# File 'lib/storefront/helpers/content_helper.rb', line 191

def encoded_contents(path)
  Base64.encode64(read_binary_file(path)).gsub(/\n/, '')
end

- (Object) font_face_data_uri(path)



195
196
197
# File 'lib/storefront/helpers/content_helper.rb', line 195

def font_face_data_uri(path)
  "url(\"data:font/truetype;base64,#{encoded_contents(path)}\")"
end

- (Object) html5_time(date_or_time)



205
206
207
208
# File 'lib/storefront/helpers/content_helper.rb', line 205

def html5_time(date_or_time)
  return nil if date_or_time.blank?
  date_or_time.acts_like?(:time) ? date_or_time.xmlschema : date_or_time.rfc3339
end

- (Object) read_binary_file(path)



187
188
189
# File 'lib/storefront/helpers/content_helper.rb', line 187

def read_binary_file(path)
  File.open(path, 'rb') {|f| f.read }
end

- (Object) read_image_size(path)



200
201
202
203
# File 'lib/storefront/helpers/content_helper.rb', line 200

def read_image_size(path)
  width, height = IO.read("#{Rails.root}/public/images/#{path}")[0x10..0x18].unpack('NN')
  {:width => width, :height => height}
end

- (Object) rendered_text(string)



172
173
174
175
176
177
178
179
180
181
# File 'lib/storefront/helpers/content_helper.rb', line 172

def rendered_text(string)
  string.to_s.
    gsub(/<\/?[^>]*>/, '').
    gsub(/\n/, " ").
    gsub("\r", " ").
    squeeze(" ").
    gsub(/\"/, "&Prime;"). # convert quotes to inches
    gsub(/\'/, "&prime;"). # single quote to feet
    gsub(/\$0?0*\.(\d+)/, "\\1&cent;").html_safe
end

- (Object) sanitize(text)



183
184
185
# File 'lib/storefront/helpers/content_helper.rb', line 183

def sanitize(text)
  text.gsub(/<\/?[^>]*>/, '').gsub(/\n/, " ").gsub("\r", " ").squeeze(" ")
end

- (Boolean) t?(key, options = {})

t?(:"what.summary", :scope => :"reports.titles", :model => deal)



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/storefront/helpers/content_helper.rb', line 44

def t?(key, options = {})
  return key if key.is_a?(::String) || key.blank?

  models      = Storefront::Model.model_names(options[:model] || options[:models] || options[:model_names] || storefront_config.current_scope[:resource] || [])
  models      << "" if models.blank?
  namespaces  = Array(options[:namespaces] || options[:namespace] || storefront_config.current_scope[:namespace])
  namespaces  << "" if namespaces.blank? && !!storefront_config.localize_with_namespace
  action_name = options[:action] || storefront_config.current_scope[:action]
  defaults    = []
  allow_blank = options[:allow_blank] == true
  count       = options[:count]
  # none, one, many, other
  count_key   = count != 1 ? :other : :one

  keys        = [key.to_s]
  
  if options.has_key?(:present) || options.has_key?(:past) || options.has_key?(:future)
    if options[:future] == true
      tense_key = :future
    else
      tense_key = (options[:present] == true || options[:past] == false) ? :present : :past
    end
  else
    tense_key = nil
  end

  if options.has_key?(:nested_model)
    with_nested_model = !!options[:nested_model]
  else
    with_nested_model = !!storefront_config.localize_with_nested_model
  end

  if options.has_key?(:namespace)
    with_namespace = !!options[:namespace]
  else
    with_namespace = !!storefront_config.localize_with_namespace
  end

  if with_nested_model && with_namespace
    # TODO, need to performance test before we do this many operations
    SCOPES_WITH_NAMESPACE_AND_NESTED_MODEL
  elsif with_nested_model
    # TODO
    SCOPES_WITH_NESTED_MODEL
  elsif with_namespace
    namespaces.each do |namespace|
      keys.each do |_key|
        SCOPES_WITH_NAMESPACE.each do |_path|
          models.each do |model|            
            path = _path.dup
            path.gsub!('%{namespace}', namespace)
            path.gsub!('%{model}', model)
            path.gsub!('%{action}', action_name)
            path.gsub!('%{attribute}', _key)
            path.gsub!('..', '.')
            defaults << path
          end
        end
      end
    end
  else
    keys.each do |_key|
      SCOPES.each do |_path|
        models.each do |model|
          path = _path.dup
          path.gsub!('%{model}', model)
          path.gsub!('%{action}', action_name)
          path.gsub!('%{attribute}', _key)
          path.gsub!('..', '.')
          defaults << path
        end
      end
    end
  end

  # RESERVED KEYS
  [:past, :present, :future, :allow_blank, :model, :models, :model_names, :action, :nested_model, :namespace, :namespaces].each do |key|
    options.delete(key)
  end

  defaults.map!(&:to_sym)
  defaults << ''

  value       = I18n.t(defaults.shift, options.merge(:default => defaults))

  if value.is_a?(::Hash)
    if value.has_key?(count_key)
      value = value[count_key]
    elsif value.has_key?(tense_key)
      value = value[tense_key]
      if value.is_a?(::Hash) && value.has_key?(count_key)
        value = value[count_key]
      end
    elsif value.has_key?(count_key)
      value = value[count_key]
    end
  end

  if value.is_a?(::Hash)
    if value.has_key?(:one)
      value = value[:one]
    end
  end

  if value.is_a?(::Hash)
    if value.has_key?(:present)
      value = value[:present]
    elsif value.has_key?(:past)
      value = value[:past]
    elsif value.has_key?(:future)
      value = value[:future]
    end
  end

  # MORE RESERVED KEYS
  [:scope].each do |key|
    options.delete(key)
  end

  if value.blank? && !allow_blank
    key.to_s.split(".").last.titleize
  elsif options.present?
    I18n.interpolate(value, options)
  else
    value
  end
end