Module: Storefront::Helpers::RequestHelper
- Defined in:
- lib/storefront/helpers/request_helper.rb
Class Method Summary collapse
-
.ensure_domain(options = {}) ⇒ Object
ensure_domain :production => “site.com”, :staging => “site-development.heroku.com”.
- .ensure_domain_options ⇒ Object
Instance Method Summary collapse
-
#browser_is?(name) ⇒ Boolean
check the current browser (via user agent) for the following: :mozilla / :firefox :ie6 :ie7 :ie :iphone :safari.
-
#browser_name ⇒ Object
find the current browser name.
- #configure_storefront ⇒ Object
- #current_subdomain ⇒ Object
- #ensure_domain ⇒ Object
- #ip_address ⇒ Object
- #link_to_sort(title, attribute) ⇒ Object
-
#linux? ⇒ Boolean
Works on Debian and Ubuntu, don’t have anything else to test on.
-
#mac? ⇒ Boolean
Works on my MacBook Pro, don’t have anything else to test on,.
- #on_current_page?(options) ⇒ Boolean
-
#opposite_sort_direction(key) ⇒ Object
gives you the other direction from the current, if exists.
- #path_namespace ⇒ Object
- #path_namespace?(*args) ⇒ Boolean
- #render_not_found(exception) ⇒ Object
-
#requesting?(expression) ⇒ Boolean
requesting?(:deals) requesting?(“/admin/users/:id”) requesting?(“admin/users#show”).
-
#sort_class(key, ascending = "ascending", descending = "descending", none = nil) ⇒ Object
sort_class(:name, “asc”, “desc”).
- #sort_direction(key) ⇒ Object
- #sort_hash ⇒ Object
- #sort_value(attribute = nil, operator = "+") ⇒ Object
- #storefront_config ⇒ Object
- #url_for(options = {}) ⇒ Object
- #user_agent ⇒ Object
- #windows? ⇒ Boolean
- #with_current_params(path_method, *args) ⇒ Object
- #with_params(*args) ⇒ Object
- #with_subdomain(subdomain) ⇒ Object
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( = {}) @ensure_domain_options = .symbolize_keys true end |
.ensure_domain_options ⇒ Object
136 137 138 |
# File 'lib/storefront/helpers/request_helper.rb', line 136 def self. @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
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_name ⇒ Object
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_storefront ⇒ Object
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_subdomain ⇒ Object
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_domain ⇒ Object
129 130 131 132 133 134 |
# File 'lib/storefront/helpers/request_helper.rb', line 129 def ensure_domain domain = self.class.[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_address ⇒ Object
88 89 90 |
# File 'lib/storefront/helpers/request_helper.rb', line 88 def ip_address request.env['REMOTE_ADDR'] end |
#link_to_sort(title, attribute) ⇒ Object
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.
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,
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
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?() url_string = CGI.unescapeHTML(url_for()) 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_namespace ⇒ Object
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
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”)
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_hash ⇒ Object
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_config ⇒ Object
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( = {}) if .kind_of?(Hash) && .has_key?(:subdomain) [:host] = with_subdomain(.delete(:subdomain)) end if .is_a?(Symbol) && == :up request.path.split("/")[0..-2].join("/") else super() end end |
#user_agent ⇒ Object
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
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.)) 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 || {} = args.shift || {} params = query_params.merge(new_params.stringify_keys) params = params.except([:except]) if [:except].present? params = params.slice([:only]) if [: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 |