My favorites | Sign in
Project Home Downloads Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
  Advanced search   Search tips   Subscriptions

Issue 156 attachment: product_slug_patch_delimiter (6.4 KB)

1
2
3
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
Index: test/fixtures/preferences.yml
===================================================================
--- test/fixtures/preferences.yml (revision 163)
+++ test/fixtures/preferences.yml (working copy)
@@ -69,3 +69,11 @@
use_smtp_tls_patch:
name: use_smtp_tls_patch
value: "0"
+
+store_use_product_slugs:
+ name: store_use_product_slugs
+ value: "1"
+
+store_product_slug_delimiter:
+ name: store_product_slug_delimiter
+ value: "--"
Index: app/helpers/application_helper.rb
===================================================================
--- app/helpers/application_helper.rb (revision 163)
+++ app/helpers/application_helper.rb (working copy)
@@ -61,4 +61,26 @@
return false
end

+ # This method generates a properly slugged link if the option is enabled
+ # Example:
+ # Produce name: Holy Hand Grenade of Antioch
+ # Product code: HOLY_GRENADE
+ # Link: /store/show/HOLY_GRENADE--holy-hand-grenade-of-antioch
+ # Adapted from method by Obie Fernandez: http://jroller.com/obie/entry/seo_optimization_of_urls_in
+ def link_to_product(name, code, image=nil)
+ if Preference.find_by_name('store_use_product_slugs').is_true?
+ slug = "#{code}" + Preference.find_by_name('store_product_slug_delimiter').value + "#{name.downcase.gsub(/[^[:alnum:]]/,'-')}".gsub(/-{2,}/,'-')
+ if(image)
+ link_to(image, :controller => 'store', :action => 'show', :id => slug)
+ else
+ link_to(name, :controller => 'store', :action => 'show', :id => slug)
+ end
+ else
+ if(image)
+ link_to(image, :controller => 'store', :action => 'show', :id => code)
+ else
+ link_to(name, :controller => 'store', :action => 'show', :id => code)
+ end
+ end
+ end
end
Index: app/controllers/store_controller.rb
===================================================================
--- app/controllers/store_controller.rb (revision 163)
+++ app/controllers/store_controller.rb (working copy)
@@ -123,7 +123,14 @@
end

def show
- @product = Product.find_by_code(params[:id])
+ # To use the slug, we have to use just a substring of params[:id]
+ if Preference.find_by_name('store_use_product_slugs').is_true?
+ delim = Preference.find_by_name('store_product_slug_delimiter').value
+ product_code = params[:id].split(delim).first
+ @product = Product.find_by_code(product_code)
+ else
+ @product = Product.find_by_code(params[:id])
+ end
if !@product
flash[:notice] = "Sorry, we couldn't find the product you were looking for"
redirect_to :action => 'index' and return false
Index: app/views/admin/preferences/index.rhtml
===================================================================
--- app/views/admin/preferences/index.rhtml (revision 163)
+++ app/views/admin/preferences/index.rhtml (working copy)
@@ -58,9 +58,41 @@
After login, their address information will be auto-filled in the checkout form.
</span>
</p>
+ <p>
+ <label>
+ <%= check_box_tag('prefs[store_use_product_slugs]', 1, (@prefs['store_use_product_slugs'].is_true?), {:id => 'slug_chckbx', :onchange => "toggleShow('slug_show_hide')"} ) %>
+ Use product slugs?
+ </label>
+ <span class="info">
+ If set, all product URLs will have the full product name appended in the form of a "slug". You shouldn't need to change the delimiter, but if it conflicts with your product code format, you may alter it below.
+ </span>
+ </p>
+ <p id="slug_show_hide">
+ <input name="prefs[store_use_product_slugs]" type="hidden" value="0" />
+ <%= make_label('Product Slug Delimiter', true) %>
+ <%= text_field_tag('prefs[store_product_slug_delimiter]', @prefs['store_product_slug_delimiter'].value, :class => 'textInput' ) %>
</div>
</div>

+ <script type="text/javascript">
+ // toggleShow function hides or shows given tag id
+ // TODO: A slider effect might be a nice thing to add
+ toggleShow = function(field_id) {
+ var element = $(field_id);
+ if(element.hasClassName('__hidden')) {
+ element.show();
+ element.removeClassName('__hidden');
+ } else {
+ element.hide();
+ element.addClassName('__hidden');
+ }
+
+ }
+ if(!(<%=@prefs['store_use_product_slugs'].is_true?%>)) {
+ toggleShow('slug_show_hide');
+ }
+ </script>
+
<div class="floatHalf">
<h2>Credit Card Processing</h2>

Index: app/views/store/_product.rhtml
===================================================================
--- app/views/store/_product.rhtml (revision 163)
+++ app/views/store/_product.rhtml (working copy)
@@ -17,10 +17,10 @@
image = image_tag(product.images[0].public_filename(:small), :alt => product.name)
end
%>
- <%= link_to(image, :controller=>'store', :action=>'show', :id => product.code) %>
+ <%= link_to_product(product.name, product.code, image) %>
</div>
<div class="product_title">
- <%= link_to(product.name, :controller=>'store', :action=>'show', :id => product.code) %>
+ <%= link_to_product(product.name, product.code) %>
</div>
<% if product.display_price? %>
<span class="money"><%= sub_number_to_currency(product.display_price) %></span>
Index: db/migrate/033_product_slug_support.rb
===================================================================
--- db/migrate/033_product_slug_support.rb (revision 0)
+++ db/migrate/033_product_slug_support.rb (revision 0)
@@ -0,0 +1,15 @@
+# Update preferences table, so that the new product slug feature may be used
+
+class ProductSlugSupport < ActiveRecord::Migration
+ def self.up
+ Preference.create([
+ { :name => 'store_use_product_slugs', :value => true },
+ { :name => 'store_product_slug_delimiter', :value => '--' }
+ ])
+ end
+
+ def self.down
+ Preference.destroy_all("name = 'store_use_product_slugs'")
+ Preference.destroy_all("name = 'store_product_slug_delimiter'")
+ end
+end
Index: db/bootstrap/preferences.yml
===================================================================
--- db/bootstrap/preferences.yml (revision 163)
+++ db/bootstrap/preferences.yml (working copy)
@@ -70,4 +70,12 @@
use_smtp_tls_patch:
name: use_smtp_tls_patch
id: 18
- value: "0"
\ No newline at end of file
+ value: "0"
+store_use_product_slugs:
+ name: store_use_product_slugs
+ id: 19
+ value: "1"
+store_product_slug_delimiter:
+ name: store_product_slug_delimiter
+ id: 20
+ value: "--"
Powered by Google Project Hosting