Module: Storefront::Helpers::RequestHelper

Defined in:
lib/storefront/helpers/request_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ensure_domain(options = {}) ⇒ Object

ensure_domain :production => “site.com”, :staging => “site-development.heroku.com”



124
125
126
127
# File 'lib/storefront/helpers/request_helper.rb', line 124

def self.ensure_domain(options = {})
  @ensure_domain_options = options.symbolize_keys
  true
end

.ensure_domain_optionsObject



136
137
138
# File 'lib/storefront/helpers/request_helper.rb', line 136

def self.ensure_domain_options
  @ensure_domain_options
end

Instance Method Details

#browser_is?(name) ⇒ Boolean

check the current browser (via user agent) for the following: :mozilla / :firefox :ie6 :ie7 :ie :iphone :safari

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/storefront/helpers/request_helper.rb', line 50

def browser_is? name
  name = name.to_s.strip

  return true if browser_name == name
  return true if (name == 'mozilla' or name == "firefox") && browser_name == 'gecko'
  return true if name == 'ie6' && browser_name.index('ie6')
  return true if name == 'ie7' && browser_name.index('ie7')
  return true if name == 'ie' && browser_name.index('ie')
  return true if name == 'iphone' && browser_name == 'iphone'
  return true if name == 'webkit' && browser_name == 'safari'

  false
end

#browser_nameObject

find the current browser name



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/storefront/helpers/request_helper.rb', line 65

def browser_name
  @browser_name ||= begin
    ua = request.user_agent.to_s.downcase
    if ua.index('msie') && !ua.index('opera') && !ua.index('webtv')
      'ie'+ua[ua.index('msie')+5].chr
    elsif ua.index('gecko/') 
      'gecko'
    elsif ua.index('opera')
      'opera'
    elsif ua.index('konqueror') 
      'konqueror'
    elsif ua.index('iphone')
      'iphone'
    elsif ua.index('applewebkit/')
      'safari'
    elsif ua.index('mozilla/')
      'gecko'
    else
      ""
    end
  end
end

#configure_storefrontObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/storefront/helpers/request_helper.rb', line 4

def configure_storefront
  action    = params[:action].to_s

  begin
    resource_object  = self.resource rescue nil
    resource_object  ||= resource_instance_name.to_s.camelize.constantize rescue nil
  rescue Exception => e
    resource_object  = nil
  end
  begin
    parent_object    = self.parent rescue nil
  rescue Exception => e
    parent_object    = nil
  end

  namespace = params[:controller].split("/")
  namespace = namespace.length > 1 ? namespace[0..-2].join(".") : nil

  storefront_config.current_scope = {
    :action    => action,
    :resource  => resource_object,
    :parent    => parent_object,
    :namespace => namespace,
    :context   => self
  }
end

#current_subdomainObject



174
175
176
177
178
# File 'lib/storefront/helpers/request_helper.rb', line 174

def current_subdomain
  subdomain = request.subdomain.to_s.split(".").first
  subdomain = nil if subdomain == App.name
  (subdomain.blank? || subdomain == "www") ? nil : subdomain
end

#ensure_domainObject



129
130
131
132
133
134
# File 'lib/storefront/helpers/request_helper.rb', line 129

def ensure_domain
  domain = self.class.ensure_domain_options[Rails.env.to_sym]
  return true if domain.blank?
  redirect_to(request.protocol + domain + request.request_uri) if request.env['HTTP_HOST'] != domain
  true
end

#ip_addressObject



88
89
90
# File 'lib/storefront/helpers/request_helper.rb', line 88

def ip_address
  request.env['REMOTE_ADDR']
end


203
204
205
206
# File 'lib/storefront/helpers/request_helper.rb', line 203

def link_to_sort(title, attribute)
  sort_param = sort_value(attribute, opposite_sort_direction(attribute))
  link_to title, with_params(request.path, :sort => sort_param)
end

#linux?Boolean

Works on Debian and Ubuntu, don’t have anything else to test on.

Returns:

  • (Boolean)


102
103
104
# File 'lib/storefront/helpers/request_helper.rb', line 102

def linux?
  (/linux/).match(RUBY_PLATFORM) ? true : false
end

#mac?Boolean

Works on my MacBook Pro, don’t have anything else to test on,

Returns:

  • (Boolean)


107
108
109
# File 'lib/storefront/helpers/request_helper.rb', line 107

def mac?
  (/darwin/).match(RUBY_PLATFORM) ? true : false
end

#on_current_page?(options) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/storefront/helpers/request_helper.rb', line 140

def on_current_page?(options)
  url_string = CGI.unescapeHTML(url_for(options))
  request = controller.request
  if url_string.index("?")
    fullpath = request.fullpath
  else
    fullpath = request.fullpath.split('?').first
  end
  if url_string =~ /^\w+:\/\//
    url_string == "#{request.protocol}#{request.host_with_port}#{fullpath}"
  else
    url_string == fullpath.gsub(/&sort_by=.*/, "").gsub(/&vendor_name=.*/, "")
  end
end

#opposite_sort_direction(key) ⇒ Object

gives you the other direction from the current, if exists



209
210
211
212
213
# File 'lib/storefront/helpers/request_helper.rb', line 209

def opposite_sort_direction(key)
  direction = sort_direction(key)
  return "+" unless direction
  direction == "+" ? "-" : "+"
end

#path_namespaceObject



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/storefront/helpers/request_helper.rb', line 184

def path_namespace
  if @path_namespace.nil?
    if current_subdomain.present?
      @path_namespace = current_subdomain
    elsif params[:path_namespace]
      @path_namespace = params[:path_namespace]
    else
      parts = params[:controller].split("/")
      @path_namespace = parts.length > 1 ? parts.first : ""
    end
  end

  @path_namespace
end

#path_namespace?(*args) ⇒ Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/storefront/helpers/request_helper.rb', line 180

def path_namespace?(*args)
  args.present? ? args.any? { |is| path_namespace == is.to_s } : path_namespace.present?
end

#render_not_found(exception) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/storefront/helpers/request_helper.rb', line 35

def render_not_found(exception)
  Exceptional::Catcher.handle_with_controller(exception, self, request) if defined?(::Exceptional)
  logger.error(exception)
  puts exception.inspect
  puts exception.backtrace.join("\n")
  render :template => "/error/404.html", :status => 404, :layout => false
end

#requesting?(expression) ⇒ Boolean

requesting?(:deals) requesting?(“/admin/users/:id”) requesting?(“admin/users#show”)

Returns:

  • (Boolean)


114
115
116
117
118
119
120
121
# File 'lib/storefront/helpers/request_helper.rb', line 114

def requesting?(expression)
  case expression
  when ::Symbol
    (request.path =~ /#{expression}\/?$/).present?
  else
    (request.path =~ expression).present?
  end
end

#sort_class(key, ascending = "ascending", descending = "descending", none = nil) ⇒ Object

sort_class(:name, “asc”, “desc”)



216
217
218
219
220
221
222
223
224
225
# File 'lib/storefront/helpers/request_helper.rb', line 216

def sort_class(key, ascending = "ascending", descending = "descending", none = nil)
  case sort_direction(key)
  when nil
    none
  when "+"
    ascending
  when "-"
    descending
  end
end

#sort_direction(key) ⇒ Object



227
228
229
230
# File 'lib/storefront/helpers/request_helper.rb', line 227

def sort_direction(key)
  return nil if params["sort"].blank?
  params["sort"][key.to_s]
end

#sort_hashObject



232
233
234
# File 'lib/storefront/helpers/request_helper.rb', line 232

def sort_hash
  {}
end

#sort_value(attribute = nil, operator = "+") ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/storefront/helpers/request_helper.rb', line 236

def sort_value(attribute = nil, operator = "+")
  result = []
  attribute = attribute.to_s
  sort_hash.each do |key, value|
    if attribute.present? && attribute == key
      result << "#{key}#{operator}"
    else
      result << "#{key}#{value}"
    end
  end
  if attribute.present? && !sort_hash.has_key?(attribute)
    result << "#{attribute}#{operator}"
  end
  result.join(",")
end

#storefront_configObject



31
32
33
# File 'lib/storefront/helpers/request_helper.rb', line 31

def storefront_config
  Storefront.configuration
end

#url_for(options = {}) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/storefront/helpers/request_helper.rb', line 162

def url_for(options = {})
  if options.kind_of?(Hash) && options.has_key?(:subdomain)  
    options[:host] = with_subdomain(options.delete(:subdomain))  
  end

  if options.is_a?(Symbol) && options == :up
    request.path.split("/")[0..-2].join("/")
  else
    super(options)
  end
end

#user_agentObject



92
93
94
# File 'lib/storefront/helpers/request_helper.rb', line 92

def user_agent
  @user_agent ||= UserAgent.parse(request.user_agent)
end

#windows?Boolean

Returns:

  • (Boolean)


96
97
98
99
# File 'lib/storefront/helpers/request_helper.rb', line 96

def windows?
  # Can't match for just 'win' cause it will match darwin as well.
  (/win32|mswin|mingw/).match(RUBY_PLATFORM) ? true : false
end

#with_current_params(path_method, *args) ⇒ Object



199
200
201
# File 'lib/storefront/helpers/request_helper.rb', line 199

def with_current_params(path_method, *args)
  send path_method, *(args << Rack::Utils.parse_query(request.query_string).merge(args.extract_options!))
end

#with_params(*args) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/storefront/helpers/request_helper.rb', line 252

def with_params(*args)
  path         = args.shift
  new_params   = args.shift || {}
  options      = args.shift || {}
  params       = query_params.merge(new_params.stringify_keys)
  params       = params.except(options[:except]) if options[:except].present?
  params       = params.slice(options[:only]) if options[:only].present?
  return path if params.blank?
  query_string = ::Deli::Query.build_query(params)

  "#{path}?#{query_string}"
end

#with_subdomain(subdomain) ⇒ Object



155
156
157
158
159
160
# File 'lib/storefront/helpers/request_helper.rb', line 155

def with_subdomain(subdomain)  
  subdomain = (subdomain || "")  
  subdomain += "." unless subdomain.empty?  
  # needs to be dynamic!
  [subdomain, request.domain, request.port_string].join
end