spree_core 1.1.6 → 1.2.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (264) hide show
  1. data/app/assets/images/{creditcards → credit_cards}/amex_cid.gif +0 -0
  2. data/app/assets/images/{creditcards/creditcard.gif → credit_cards/credit_card.gif} +0 -0
  3. data/app/assets/images/{creditcards → credit_cards}/discover_cid.gif +0 -0
  4. data/app/assets/images/{creditcards → credit_cards}/icons/american_express.png +0 -0
  5. data/app/assets/images/{creditcards → credit_cards}/icons/cirrus.png +0 -0
  6. data/app/assets/images/{creditcards → credit_cards}/icons/delta.png +0 -0
  7. data/app/assets/images/{creditcards → credit_cards}/icons/dinersclub.png +0 -0
  8. data/app/assets/images/{creditcards → credit_cards}/icons/directdebit.png +0 -0
  9. data/app/assets/images/{creditcards → credit_cards}/icons/discover.png +0 -0
  10. data/app/assets/images/{creditcards → credit_cards}/icons/egold.png +0 -0
  11. data/app/assets/images/{creditcards → credit_cards}/icons/maestro.png +0 -0
  12. data/app/assets/images/{creditcards → credit_cards}/icons/master.png +0 -0
  13. data/app/assets/images/{creditcards → credit_cards}/icons/paypal.png +0 -0
  14. data/app/assets/images/{creditcards → credit_cards}/icons/solo.png +0 -0
  15. data/app/assets/images/{creditcards → credit_cards}/icons/switch.png +0 -0
  16. data/app/assets/images/{creditcards → credit_cards}/icons/visa.png +0 -0
  17. data/app/assets/images/{creditcards → credit_cards}/icons/visaelectron.png +0 -0
  18. data/app/assets/images/{creditcards → credit_cards}/icons/westernunion.png +0 -0
  19. data/app/assets/images/{creditcards → credit_cards}/icons/wirecard.png +0 -0
  20. data/app/assets/images/{creditcards → credit_cards}/icons/worldpay.png +0 -0
  21. data/app/assets/images/{creditcards → credit_cards}/master_cid.jpg +0 -0
  22. data/app/assets/images/{creditcards → credit_cards}/visa_cid.gif +0 -0
  23. data/app/assets/javascripts/admin/admin.js.erb +12 -136
  24. data/app/assets/javascripts/admin/checkouts/edit.js +18 -35
  25. data/app/assets/javascripts/admin/gateway.js +2 -2
  26. data/app/assets/javascripts/admin/images/index.js.coffee +12 -0
  27. data/app/assets/javascripts/admin/images/new.js.coffee +4 -0
  28. data/app/assets/javascripts/admin/orders/edit.js +1 -1
  29. data/app/assets/javascripts/admin/orders/edit_form.js +0 -2
  30. data/app/assets/javascripts/admin/product_autocomplete.js.erb +116 -0
  31. data/app/assets/javascripts/store/cart.js.coffee +7 -0
  32. data/app/assets/javascripts/store/checkout.js.coffee +57 -0
  33. data/app/assets/javascripts/store/product.js.coffee +36 -0
  34. data/app/assets/stylesheets/admin/admin.css.erb +5 -0
  35. data/app/assets/stylesheets/admin/spree_core.css +1 -0
  36. data/app/assets/stylesheets/store/screen.css.scss +15 -1
  37. data/app/assets/stylesheets/store/variables.css.scss +29 -29
  38. data/app/controllers/spree/admin/banners_controller.rb +14 -0
  39. data/app/controllers/spree/admin/base_controller.rb +11 -0
  40. data/app/controllers/spree/admin/images_controller.rb +13 -5
  41. data/app/controllers/spree/admin/line_items_controller.rb +16 -25
  42. data/app/controllers/spree/admin/mail_methods_controller.rb +1 -8
  43. data/app/controllers/spree/admin/option_types_controller.rb +0 -25
  44. data/app/controllers/spree/admin/orders/customer_details_controller.rb +3 -3
  45. data/app/controllers/spree/admin/orders_controller.rb +23 -14
  46. data/app/controllers/spree/admin/payment_methods_controller.rb +1 -7
  47. data/app/controllers/spree/admin/payments_controller.rb +5 -5
  48. data/app/controllers/spree/admin/products_controller.rb +15 -5
  49. data/app/controllers/spree/admin/prototypes_controller.rb +2 -2
  50. data/app/controllers/spree/admin/reports_controller.rb +1 -7
  51. data/app/controllers/spree/admin/resource_controller.rb +130 -130
  52. data/app/controllers/spree/admin/search_controller.rb +47 -0
  53. data/app/controllers/spree/admin/taxons_controller.rb +0 -43
  54. data/app/controllers/spree/base_controller.rb +2 -1
  55. data/app/controllers/spree/checkout_controller.rb +29 -10
  56. data/app/controllers/spree/orders_controller.rb +7 -11
  57. data/app/controllers/spree/products_controller.rb +5 -1
  58. data/app/helpers/spree/account_helper.rb +4 -0
  59. data/app/helpers/spree/admin/base_helper.rb +2 -44
  60. data/app/helpers/spree/admin/navigation_helper.rb +1 -3
  61. data/app/helpers/spree/admin/orders_helper.rb +3 -2
  62. data/app/helpers/spree/admin/products_helper.rb +21 -0
  63. data/app/helpers/spree/base_helper.rb +14 -11
  64. data/app/helpers/spree/checkout_helper.rb +5 -1
  65. data/app/helpers/spree/orders_helper.rb +5 -0
  66. data/app/helpers/spree/products_helper.rb +4 -0
  67. data/app/helpers/spree/trackers_helper.rb +4 -0
  68. data/app/models/spree/ability.rb +70 -0
  69. data/app/models/spree/address.rb +4 -12
  70. data/app/models/spree/adjustment.rb +30 -8
  71. data/app/models/spree/app_configuration.rb +1 -1
  72. data/app/models/spree/calculator/price_sack.rb +3 -5
  73. data/app/models/spree/country.rb +1 -4
  74. data/app/models/spree/{creditcard.rb → credit_card.rb} +4 -4
  75. data/app/models/spree/gateway.rb +2 -2
  76. data/app/models/spree/gateway/bogus.rb +10 -10
  77. data/app/models/spree/gateway/bogus_simple.rb +4 -4
  78. data/app/models/spree/image.rb +3 -12
  79. data/app/models/spree/inventory_unit.rb +4 -6
  80. data/app/models/spree/{user.rb → legacy_user.rb} +7 -6
  81. data/app/models/spree/line_item.rb +4 -5
  82. data/app/models/spree/option_type.rb +4 -6
  83. data/app/models/spree/option_value.rb +1 -1
  84. data/app/models/spree/order.rb +90 -132
  85. data/app/models/spree/order/checkout.rb +124 -0
  86. data/app/models/spree/payment.rb +6 -6
  87. data/app/models/spree/payment/processing.rb +34 -38
  88. data/app/models/spree/payment_method.rb +2 -2
  89. data/app/models/spree/preference.rb +2 -0
  90. data/app/models/spree/preferences/preferable_class_methods.rb +0 -2
  91. data/app/models/spree/preferences/store.rb +3 -21
  92. data/app/models/spree/product.rb +41 -43
  93. data/app/models/spree/product/scopes.rb +9 -25
  94. data/app/models/spree/product_option_type.rb +2 -2
  95. data/app/models/spree/product_property.rb +9 -4
  96. data/app/models/spree/property.rb +1 -1
  97. data/app/models/spree/prototype.rb +2 -2
  98. data/app/models/spree/return_authorization.rb +2 -2
  99. data/app/models/spree/role.rb +1 -1
  100. data/app/models/spree/shipment.rb +6 -5
  101. data/app/models/spree/shipping_method.rb +19 -13
  102. data/app/models/spree/state.rb +2 -5
  103. data/app/models/spree/state_change.rb +1 -1
  104. data/app/models/spree/tax_rate.rb +8 -1
  105. data/app/models/spree/taxon.rb +5 -1
  106. data/app/models/spree/taxonomy.rb +1 -1
  107. data/app/models/spree/tokenized_permission.rb +6 -0
  108. data/app/models/spree/tracker.rb +1 -1
  109. data/app/models/spree/variant.rb +13 -40
  110. data/app/models/spree/zone.rb +15 -14
  111. data/app/models/spree/zone_member.rb +1 -1
  112. data/app/views/spree/admin/image_settings/edit.html.erb +2 -2
  113. data/app/views/spree/admin/images/_form.html.erb +8 -4
  114. data/app/views/spree/admin/images/index.html.erb +1 -1
  115. data/app/views/spree/admin/line_items/create.js.erb +1 -1
  116. data/app/views/spree/admin/option_types/_option_value_fields.html.erb +1 -1
  117. data/app/views/spree/admin/option_types/index.html.erb +1 -1
  118. data/app/views/spree/admin/orders/_add_product.html.erb +1 -1
  119. data/app/views/spree/admin/orders/customer_details/edit.html.erb +1 -1
  120. data/app/views/spree/admin/orders/index.html.erb +4 -4
  121. data/app/views/spree/admin/orders/show.html.erb +1 -1
  122. data/app/views/spree/admin/payments/_form.html.erb +1 -1
  123. data/app/views/spree/admin/payments/new.html.erb +0 -4
  124. data/app/views/spree/admin/payments/source_forms/_gateway.html.erb +1 -1
  125. data/app/views/spree/admin/payments/source_views/_gateway.html.erb +2 -2
  126. data/app/views/spree/admin/products/_form.html.erb +21 -5
  127. data/app/views/spree/admin/products/index.html.erb +2 -2
  128. data/app/views/spree/admin/properties/_form.html.erb +1 -1
  129. data/app/views/spree/admin/properties/index.html.erb +1 -1
  130. data/app/views/spree/admin/return_authorizations/_form.html.erb +33 -17
  131. data/app/views/spree/admin/return_authorizations/edit.html.erb +2 -2
  132. data/app/views/spree/admin/shared/_address_form.html.erb +1 -1
  133. data/app/views/spree/admin/shared/_head.html.erb +12 -2
  134. data/app/views/spree/admin/shared/_order_tabs.html.erb +1 -4
  135. data/app/views/spree/admin/shared/_product_tabs.html.erb +0 -6
  136. data/app/views/spree/admin/shared/_show_resource_links.html.erb +3 -3
  137. data/app/views/spree/admin/shared/_tabs.html.erb +1 -2
  138. data/app/views/spree/admin/shipments/edit.html.erb +2 -2
  139. data/app/views/spree/admin/shipping_methods/_form.html.erb +1 -1
  140. data/app/views/spree/admin/states/_state_list.html.erb +1 -1
  141. data/app/views/spree/admin/tax_rates/index.html.erb +1 -1
  142. data/app/views/spree/admin/taxonomies/_form.html.erb +1 -1
  143. data/app/views/spree/admin/taxons/_form.html.erb +1 -1
  144. data/app/views/spree/admin/taxons/_taxon_table.html.erb +2 -2
  145. data/app/views/spree/admin/variants/_form.html.erb +13 -13
  146. data/app/views/spree/admin/variants/index.html.erb +1 -7
  147. data/app/views/spree/checkout/_address.html.erb +10 -4
  148. data/app/views/spree/checkout/edit.html.erb +0 -1
  149. data/app/views/spree/checkout/payment/_gateway.html.erb +5 -5
  150. data/app/views/spree/checkout/registration.html.erb +2 -2
  151. data/app/views/spree/content/cvv.html.erb +6 -6
  152. data/app/views/spree/layouts/spree_application.html.erb +4 -48
  153. data/app/views/spree/order_mailer/cancel_email.text.erb +5 -5
  154. data/app/views/spree/order_mailer/confirm_email.text.erb +6 -7
  155. data/app/views/spree/orders/_adjustments.html.erb +14 -0
  156. data/app/views/spree/orders/_form.html.erb +1 -0
  157. data/app/views/spree/orders/show.html.erb +3 -3
  158. data/app/views/spree/products/_thumbnails.html.erb +1 -1
  159. data/app/views/spree/products/index.html.erb +1 -1
  160. data/app/views/spree/shared/_footer.html.erb +6 -0
  161. data/app/views/spree/shared/_google_analytics.html.erb +15 -16
  162. data/app/views/spree/shared/_head.html.erb +3 -9
  163. data/app/views/spree/shared/_header.html.erb +5 -0
  164. data/app/views/spree/shared/_main_nav_bar.html.erb +6 -0
  165. data/app/views/spree/shared/_nav_bar.html.erb +7 -3
  166. data/app/views/spree/shared/_order_details.html.erb +14 -11
  167. data/app/views/spree/shared/_sidebar.html.erb +3 -0
  168. data/app/views/spree/shared/unauthorized.html.erb +0 -0
  169. data/app/views/spree/shipment_mailer/shipped_email.text.erb +7 -7
  170. data/app/views/spree/taxons/show.html.erb +1 -1
  171. data/config/initializers/check_for_orphaned_preferences.rb +1 -1
  172. data/config/initializers/spree.rb +0 -12
  173. data/config/initializers/user_class_extensions.rb +25 -0
  174. data/config/initializers/workarounds_for_ruby19.rb +72 -0
  175. data/config/locales/en.yml +11 -24
  176. data/config/routes.rb +7 -37
  177. data/db/migrate/20090823005402_spree_zero_nine_zero.rb +14 -12
  178. data/db/migrate/20091015153048_add_openid_field_to_users.rb +9 -7
  179. data/db/migrate/20100209144531_polymorphic_payments.rb +5 -5
  180. data/db/migrate/20100214212536_assign_creditcard_txns_to_payment.rb +4 -4
  181. data/db/migrate/20100528185820_add_index_on_users_persistence_token.rb +3 -1
  182. data/db/migrate/20100811163637_add_guest_flag.rb +3 -1
  183. data/db/migrate/20100901171814_change_guest_flag_to_anonymous.rb +3 -1
  184. data/db/migrate/20101026184959_generate_anonymous_users.rb +3 -3
  185. data/db/migrate/20101103212716_drop_anonymous_field_for_user.rb +3 -1
  186. data/db/migrate/20111007143030_namespace_top_level_models.rb +18 -1
  187. data/db/migrate/20120315064358_migrate_images_from_products_to_variants.rb +6 -4
  188. data/db/migrate/20120509055454_create_tokenized_permissions_table.rb +16 -0
  189. data/db/migrate/20120530012000_rename_creditcards_to_credit_cards.rb +11 -0
  190. data/db/migrate/20120604203654_remove_credit_total_from_orders.rb +5 -0
  191. data/lib/generators/spree/custom_user/custom_user_generator.rb +53 -0
  192. data/lib/generators/spree/custom_user/templates/authentication_helpers.rb.tt +28 -0
  193. data/lib/generators/spree/custom_user/templates/initializer.rb.tt +1 -0
  194. data/lib/generators/spree/custom_user/templates/migration.rb.tt +7 -0
  195. data/lib/generators/spree/dummy/dummy_generator.rb +3 -0
  196. data/lib/generators/spree/dummy/templates/initializers/custom_user.rb +1 -0
  197. data/lib/generators/spree/install/templates/app/assets/javascripts/admin/all.js +0 -1
  198. data/lib/generators/spree/install/templates/app/assets/javascripts/store/all.js +0 -1
  199. data/lib/generators/spree/install/templates/app/assets/stylesheets/admin/all.css +0 -1
  200. data/lib/generators/spree/install/templates/app/assets/stylesheets/store/all.css +0 -1
  201. data/lib/generators/spree/install/templates/config/initializers/spree.rb +2 -0
  202. data/lib/spree/core.rb +15 -2
  203. data/lib/spree/core/controller_helpers.rb +123 -62
  204. data/lib/spree/core/current_order.rb +5 -0
  205. data/lib/spree/core/engine.rb +1 -1
  206. data/lib/spree/core/mail_settings.rb +1 -2
  207. data/lib/spree/core/permalinks.rb +1 -5
  208. data/lib/spree/core/preference_rescue.rb +8 -4
  209. data/lib/spree/core/relation_serialization.rb +9 -0
  210. data/lib/spree/core/respond_with.rb +1 -1
  211. data/lib/spree/core/s3_support.rb +25 -0
  212. data/lib/spree/core/search/base.rb +2 -5
  213. data/lib/spree/core/ssl_requirement.rb +2 -2
  214. data/lib/spree/core/store_helpers.rb +1 -2
  215. data/lib/spree/core/testing_support/controller_requests.rb +1 -0
  216. data/lib/spree/core/testing_support/env.rb +2 -0
  217. data/lib/spree/core/testing_support/factories/{creditcard_factory.rb → credit_card_factory.rb} +3 -3
  218. data/lib/spree/core/testing_support/factories/payment_factory.rb +1 -1
  219. data/lib/spree/core/testing_support/factories/product_factory.rb +4 -4
  220. data/lib/spree/core/testing_support/factories/user_factory.rb +3 -3
  221. data/lib/spree/core/testing_support/factories/zone_factory.rb +2 -3
  222. data/lib/spree/core/token_resource.rb +27 -0
  223. data/lib/spree/core/version.rb +1 -1
  224. data/lib/spree/product_filters.rb +12 -13
  225. data/lib/tasks/core.rake +1 -1
  226. metadata +146 -154
  227. data/app/assets/images/noimage/large.png +0 -0
  228. data/app/assets/javascripts/admin/images/index.js +0 -10
  229. data/app/assets/javascripts/admin/images/new.js +0 -5
  230. data/app/assets/javascripts/admin/products.js +0 -2
  231. data/app/assets/javascripts/store/cart.js +0 -11
  232. data/app/assets/javascripts/store/checkout.js +0 -78
  233. data/app/assets/javascripts/store/product.js +0 -49
  234. data/app/controllers/spree/admin/users_controller.rb +0 -68
  235. data/app/views/spree/admin/option_types/_available.html.erb +0 -28
  236. data/app/views/spree/admin/option_types/_selected.html.erb +0 -26
  237. data/app/views/spree/admin/option_types/available.js.erb +0 -2
  238. data/app/views/spree/admin/option_types/select.js.erb +0 -3
  239. data/app/views/spree/admin/option_types/selected.html.erb +0 -6
  240. data/app/views/spree/admin/orders/history.html.erb +0 -29
  241. data/app/views/spree/admin/products/_option_types.html.erb +0 -40
  242. data/app/views/spree/admin/shared/_additional_field.html.erb +0 -5
  243. data/app/views/spree/admin/shared/_routes.html.erb +0 -8
  244. data/app/views/spree/admin/shared/_translations.html.erb +0 -17
  245. data/app/views/spree/admin/taxons/available.js.erb +0 -26
  246. data/app/views/spree/admin/taxons/remove.html.erb +0 -1
  247. data/app/views/spree/admin/taxons/select.js.erb +0 -2
  248. data/app/views/spree/admin/taxons/selected.html.erb +0 -42
  249. data/app/views/spree/admin/users/_form.html.erb +0 -19
  250. data/app/views/spree/admin/users/edit.html.erb +0 -14
  251. data/app/views/spree/admin/users/index.html.erb +0 -48
  252. data/app/views/spree/admin/users/new.html.erb +0 -14
  253. data/app/views/spree/admin/users/show.html.erb +0 -21
  254. data/app/views/spree/shared/_store_menu.html.erb +0 -2
  255. data/config/initializers/rails_5868.rb +0 -8
  256. data/db/migrate/20120605211305_make_users_email_index_unique.rb +0 -10
  257. data/db/migrate/20121017010007_remove_not_null_constraint_from_products_on_hand.rb +0 -11
  258. data/db/sample/users.rb +0 -61
  259. data/lib/generators/spree/sandbox/sandbox_generator.rb +0 -36
  260. data/lib/generators/spree/sandbox/templates/rails/routes.rb +0 -7
  261. data/lib/spree/core/ext/array.rb +0 -14
  262. data/lib/spree/core/ext/hash.rb +0 -75
  263. data/lib/spree/core/ext/string.rb +0 -10
  264. data/lib/tasks/install.rake +0 -18
@@ -0,0 +1,124 @@
1
+ module Spree
2
+ class Order < ActiveRecord::Base
3
+ module Checkout
4
+ def self.included(klass)
5
+ klass.class_eval do
6
+ cattr_accessor :next_event_transitions
7
+ cattr_accessor :previous_states
8
+ cattr_accessor :checkout_flow
9
+ cattr_accessor :checkout_steps
10
+
11
+ def self.checkout_flow(&block)
12
+ if block_given?
13
+ @checkout_flow = block
14
+ else
15
+ @checkout_flow
16
+ end
17
+ end
18
+
19
+ def self.define_state_machine!
20
+ self.checkout_steps = []
21
+ self.next_event_transitions = []
22
+ self.previous_states = [:cart]
23
+ instance_eval(&checkout_flow)
24
+ klass = self
25
+
26
+ state_machine :state, :initial => :cart do
27
+ klass.next_event_transitions.each { |t| transition(t.merge(:on => :next)) }
28
+
29
+ # Persist the state on the order
30
+ after_transition do |order|
31
+ order.state = order.state
32
+ order.save
33
+ end
34
+
35
+ event :cancel do
36
+ transition :to => :canceled, :if => :allow_cancel?
37
+ end
38
+
39
+ event :return do
40
+ transition :to => :returned, :from => :awaiting_return
41
+ end
42
+
43
+ event :resume do
44
+ transition :to => :resumed, :from => :canceled, :if => :allow_resume?
45
+ end
46
+
47
+ event :authorize_return do
48
+ transition :to => :awaiting_return
49
+ end
50
+
51
+ before_transition :to => :complete do |order|
52
+ begin
53
+ order.process_payments!
54
+ rescue Spree::Core::GatewayError
55
+ !!Spree::Config[:allow_checkout_on_gateway_error]
56
+ end
57
+ end
58
+
59
+ before_transition :to => :delivery, :do => :remove_invalid_shipments!
60
+
61
+ after_transition :to => :complete, :do => :finalize!
62
+ after_transition :to => :delivery, :do => :create_tax_charge!
63
+ after_transition :to => :resumed, :do => :after_resume
64
+ after_transition :to => :canceled, :do => :after_cancel
65
+
66
+ after_transition :from => :delivery, :do => :create_shipment!
67
+ end
68
+ end
69
+
70
+ def self.go_to_state(name, options={})
71
+ self.checkout_steps[name] = options
72
+ if options[:if]
73
+ previous_states.each do |state|
74
+ add_transition({:from => state, :to => name}.merge(options))
75
+ end
76
+ self.previous_states << name
77
+ else
78
+ previous_states.each do |state|
79
+ add_transition({:from => state, :to => name}.merge(options))
80
+ end
81
+ self.previous_states = [name]
82
+ end
83
+ end
84
+
85
+ def self.remove_transition(options={})
86
+ if transition = find_transition(options)
87
+ self.next_event_transitions.delete(transition)
88
+ end
89
+ end
90
+
91
+ def self.find_transition(options={})
92
+ self.next_event_transitions.detect do |transition|
93
+ transition[options[:from].to_sym] == options[:to].to_sym
94
+ end
95
+ end
96
+
97
+ def self.next_event_transitions
98
+ @next_event_transitions ||= []
99
+ end
100
+
101
+ def self.checkout_steps
102
+ @checkout_steps ||= ActiveSupport::OrderedHash.new
103
+ end
104
+
105
+ def self.add_transition(options)
106
+ self.next_event_transitions << { options.delete(:from) => options.delete(:to) }.merge(options)
107
+ end
108
+
109
+ def checkout_steps
110
+ checkout_steps = []
111
+ # TODO: replace this with each_with_object once Ruby 1.9 is standard
112
+ self.class.checkout_steps.each do |step, options|
113
+ if options[:if]
114
+ next unless options[:if].call(self)
115
+ end
116
+ checkout_steps << step
117
+ end
118
+ checkout_steps.map(&:to_s)
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
@@ -1,11 +1,11 @@
1
1
  module Spree
2
2
  class Payment < ActiveRecord::Base
3
3
  include Spree::Payment::Processing
4
- belongs_to :order, :class_name => "Spree::Order"
4
+ belongs_to :order
5
5
  belongs_to :source, :polymorphic => true, :validate => true
6
- belongs_to :payment_method, :class_name => "Spree::PaymentMethod"
6
+ belongs_to :payment_method
7
7
 
8
- has_many :offsets, :class_name => 'Spree::Payment', :foreign_key => 'source_id', :conditions => "source_type = 'Spree::Payment' AND amount < 0 AND state = 'completed'"
8
+ has_many :offsets, :class_name => "Spree::Payment", :foreign_key => :source_id, :conditions => "source_type = 'Spree::Payment' AND amount < 0 AND state = 'completed'"
9
9
  has_many :log_entries, :as => :source
10
10
 
11
11
  after_save :create_payment_profile, :if => :profiles_supported?
@@ -18,7 +18,7 @@ module Spree
18
18
 
19
19
  attr_accessible :amount, :payment_method_id, :source_attributes
20
20
 
21
- scope :from_creditcard, where(:source_type => 'Spree::Creditcard')
21
+ scope :from_credit_card, where(:source_type => 'Spree::CreditCard')
22
22
  scope :with_state, lambda { |s| where(:state => s) }
23
23
  scope :completed, with_state('completed')
24
24
  scope :pending, with_state('pending')
@@ -32,7 +32,7 @@ module Spree
32
32
  end
33
33
  # When processing during checkout fails
34
34
  event :failure do
35
- transition :from => ['processing', 'pending'], :to => 'failed'
35
+ transition :from => 'processing', :to => 'failed'
36
36
  end
37
37
  # With card payments this represents authorizing the payment
38
38
  event :pend do
@@ -90,7 +90,7 @@ module Spree
90
90
  end
91
91
 
92
92
  def create_payment_profile
93
- return unless source.is_a?(Creditcard) && source.number && !source.has_payment_profile?
93
+ return unless source.is_a?(CreditCard) && source.number && !source.has_payment_profile?
94
94
  payment_method.create_profile(self)
95
95
  rescue ActiveMerchant::ConnectionError => e
96
96
  gateway_error e
@@ -32,8 +32,8 @@ module Spree
32
32
  check_environment
33
33
 
34
34
  if payment_method.payment_profiles_supported?
35
- # Gateways supporting payment profiles will need access to creditcard object because this stores the payment profile information
36
- # so supply the authorization itself as well as the creditcard, rather than just the authorization code
35
+ # Gateways supporting payment profiles will need access to credit card object because this stores the payment profile information
36
+ # so supply the authorization itself as well as the credit card, rather than just the authorization code
37
37
  response = payment_method.capture(self, source, gateway_options)
38
38
  else
39
39
  # Standard ActiveMerchant capture usage
@@ -47,13 +47,12 @@ module Spree
47
47
  end
48
48
 
49
49
  def void_transaction!
50
- return true if void?
51
50
  protect_from_connection_error do
52
51
  check_environment
53
52
 
54
53
  if payment_method.payment_profiles_supported?
55
- # Gateways supporting payment profiles will need access to creditcard object because this stores the payment profile information
56
- # so supply the authorization itself as well as the creditcard, rather than just the authorization code
54
+ # Gateways supporting payment profiles will need access to credit card object because this stores the payment profile information
55
+ # so supply the authorization itself as well as the credit card, rather than just the authorization code
57
56
  response = payment_method.void(self.response_code, source, gateway_options)
58
57
  else
59
58
  # Standard ActiveMerchant void usage
@@ -105,40 +104,8 @@ module Spree
105
104
  credit!(amount)
106
105
  end
107
106
 
108
- def gateway_options
109
- options = { :email => order.email,
110
- :customer => order.email,
111
- :ip => '192.168.1.100', # TODO: Use an actual IP
112
- :order_id => order.number }
113
-
114
- options.merge!({ :shipping => order.ship_total * 100,
115
- :tax => order.tax_total * 100,
116
- :subtotal => order.item_total * 100 })
117
-
118
- options.merge!({ :currency => payment_method.preferences[:currency_code] }) if payment_method && payment_method.preferences[:currency_code]
119
-
120
- options.merge!({ :billing_address => order.bill_address.try(:active_merchant_hash),
121
- :shipping_address => order.ship_address.try(:active_merchant_hash) })
122
-
123
- options.merge!(:discount => promo_total) if respond_to?(:promo_total)
124
- options
125
- end
126
-
127
107
  private
128
108
 
129
- def gateway_error(error)
130
- if error.is_a? ActiveMerchant::Billing::Response
131
- text = error.params['message'] || error.params['response_reason_text'] || error.message
132
- elsif error.is_a? ActiveMerchant::ConnectionError
133
- text = I18n.t(:unable_to_connect_to_gateway)
134
- else
135
- text = error.to_s
136
- end
137
- logger.error(I18n.t(:gateway_error))
138
- logger.error(" #{error.to_yaml}")
139
- raise Core::GatewayError.new(text)
140
- end
141
-
142
109
  def gateway_action(source, action, success_state)
143
110
  protect_from_connection_error do
144
111
  check_environment
@@ -160,7 +127,7 @@ module Spree
160
127
  end
161
128
  self.send("#{success_state}!")
162
129
  else
163
- self.send("#{failure_state}!")
130
+ self.send(failure_state)
164
131
  gateway_error(response)
165
132
  end
166
133
  end
@@ -169,6 +136,22 @@ module Spree
169
136
  log_entries.create({:details => response.to_yaml}, :without_protection => true)
170
137
  end
171
138
 
139
+ def gateway_options
140
+ options = { :email => order.email,
141
+ :customer => order.email,
142
+ :ip => '192.168.1.100', # TODO: Use an actual IP
143
+ :order_id => order.number }
144
+
145
+ options.merge!({ :shipping => order.ship_total * 100,
146
+ :tax => order.tax_total * 100,
147
+ :subtotal => order.item_total * 100 })
148
+
149
+ options.merge!({ :currency => payment_method.preferences[:currency_code] }) if payment_method && payment_method.preferences[:currency_code]
150
+
151
+ options.merge({ :billing_address => order.bill_address.try(:active_merchant_hash),
152
+ :shipping_address => order.ship_address.try(:active_merchant_hash) })
153
+ end
154
+
172
155
  def protect_from_connection_error
173
156
  begin
174
157
  yield
@@ -181,6 +164,19 @@ module Spree
181
164
  log_entries.create({:details => response.to_yaml}, :without_protection => true)
182
165
  end
183
166
 
167
+ def gateway_error(error)
168
+ if error.is_a? ActiveMerchant::Billing::Response
169
+ text = error.params['message'] || error.params['response_reason_text'] || error.message
170
+ elsif error.is_a? ActiveMerchant::ConnectionError
171
+ text = I18n.t(:unable_to_connect_to_gateway)
172
+ else
173
+ text = error.to_s
174
+ end
175
+ logger.error(I18n.t(:gateway_error))
176
+ logger.error(" #{error.to_yaml}")
177
+ raise Core::GatewayError.new(text)
178
+ end
179
+
184
180
  # Saftey check to make sure we're not accidentally performing operations on a live gateway.
185
181
  # Ex. When testing in staging environment with a copy of production data.
186
182
  def check_environment
@@ -16,7 +16,7 @@ module Spree
16
16
  end
17
17
 
18
18
  # The class that will process payments for this payment type, used for @payment.source
19
- # e.g. Creditcard in the case of a the Gateway payment type
19
+ # e.g. CreditCard in the case of a the Gateway payment type
20
20
  # nil means the payment method doesn't require a source e.g. check
21
21
  def payment_source_class
22
22
  raise 'You must implement payment_source_class method for this gateway.'
@@ -27,7 +27,7 @@ module Spree
27
27
  end
28
28
 
29
29
  def self.active?
30
- self.count(:conditions => { :type => self.to_s, :environment => Rails.env, :active => true }) > 0
30
+ where(:type => self.to_s, :environment => Rails.env, :active => true).count > 0
31
31
  end
32
32
 
33
33
  def method_type
@@ -21,6 +21,8 @@ class Spree::Preference < ActiveRecord::Base
21
21
  self[:value].to_i
22
22
  when :boolean
23
23
  (self[:value].to_s =~ /^[t|1]/i) != nil
24
+ else
25
+ self[:value].is_a?(String) ? YAML.load(self[:value]) : self[:value]
24
26
  end
25
27
  else
26
28
  self[:value]
@@ -15,8 +15,6 @@ module Spree::Preferences
15
15
  else
16
16
  if get_pending_preference(name)
17
17
  get_pending_preference(name)
18
- elsif Spree::Preference.table_exists? && preference = Spree::Preference.find_by_name(name)
19
- preference.value
20
18
  else
21
19
  send self.class.preference_default_getter_method(name)
22
20
  end
@@ -23,29 +23,11 @@ module Spree::Preferences
23
23
  end
24
24
 
25
25
  def exist?(key)
26
- @cache.exist?(key) ||
27
- should_persist? && Spree::Preference.where(:key => key).exists?
26
+ @cache.exist? key
28
27
  end
29
28
 
30
29
  def get(key)
31
- # return the retrieved value, if it's in the cache
32
- if (val = @cache.read(key)).present?
33
- return val
34
- end
35
-
36
- return nil unless should_persist?
37
-
38
- # If it's not in the cache, maybe it's in the database, but
39
- # has been cleared from the cache
40
-
41
- # does it exist in the database?
42
- if preference = Spree::Preference.find_by_key(key)
43
- # it does exist, so let's put it back into the cache
44
- @cache.write(preference.key, preference.value)
45
-
46
- # and return the value
47
- preference.value
48
- end
30
+ @cache.read(key)
49
31
  end
50
32
 
51
33
  def delete(key)
@@ -58,7 +40,7 @@ module Spree::Preferences
58
40
  def persist(cache_key, value, type)
59
41
  return unless should_persist?
60
42
 
61
- preference = Spree::Preference.find_or_initialize_by_key(cache_key)
43
+ preference = Spree::Preference.where(:key => cache_key).first_or_initialize
62
44
  preference.value = value
63
45
  preference.value_type = type
64
46
  preference.save
@@ -30,19 +30,8 @@ module Spree
30
30
  belongs_to :shipping_category
31
31
 
32
32
  has_one :master,
33
- :class_name => 'Spree::Variant',
34
- :conditions => { :is_master => true },
35
- :dependent => :destroy
36
-
37
- has_many :variants,
38
- :class_name => 'Spree::Variant',
39
- :conditions => { :is_master => false, :deleted_at => nil },
40
- :order => "#{::Spree::Variant.quoted_table_name}.position ASC"
41
-
42
- has_many :variants_including_master,
43
- :class_name => 'Spree::Variant',
44
- :conditions => { :deleted_at => nil },
45
- :dependent => :destroy
33
+ :class_name => "Spree::Variant",
34
+ :conditions => { :is_master => true }
46
35
 
47
36
  delegate_belongs_to :master, :sku, :price, :weight, :height, :width, :depth, :is_master
48
37
  delegate_belongs_to :master, :cost_price if Variant.table_exists? && Variant.column_names.include?('cost_price')
@@ -54,13 +43,33 @@ module Spree
54
43
  after_save :save_master
55
44
  after_save :set_master_on_hand_to_zero_when_product_has_variants
56
45
 
57
- delegate :images, :to => :master, :prefix => true
58
- alias_method :images, :master_images
46
+ has_many :variants,
47
+ :class_name => "Spree::Variant",
48
+ :conditions => { :is_master => false, :deleted_at => nil },
49
+ :order => 'position ASC'
50
+
51
+ has_many :variants_including_master,
52
+ :class_name => "Spree::Variant",
53
+ :conditions => { :deleted_at => nil },
54
+ :dependent => :destroy
59
55
 
60
- has_many :variant_images, :source => :images, :through => :variants_including_master, :order => :position
56
+ has_many :variants_with_only_master,
57
+ :class_name => "Spree::Variant",
58
+ :conditions => { :is_master => true, :deleted_at => nil },
59
+ :dependent => :destroy
61
60
 
62
61
  accepts_nested_attributes_for :variants, :allow_destroy => true
63
62
 
63
+ def variant_images
64
+ Image.joins("LEFT JOIN #{Variant.quoted_table_name} ON #{Variant.quoted_table_name}.id = #{Spree::Asset.quoted_table_name}.viewable_id").
65
+ where("(#{Spree::Asset.quoted_table_name}.viewable_type = ? AND #{Spree::Asset.quoted_table_name}.viewable_id = ?) OR
66
+ (#{Spree::Asset.quoted_table_name}.viewable_type = ? AND #{Spree::Asset.quoted_table_name}.viewable_id = ?)", Variant.name, self.master.id, Product.name, self.id).
67
+ order("#{Spree::Asset.quoted_table_name}.position").
68
+ extend(Spree::Core::RelationSerialization)
69
+ end
70
+
71
+ alias_method :images, :variant_images
72
+
64
73
  validates :name, :price, :permalink, :presence => true
65
74
 
66
75
  attr_accessor :option_values_hash
@@ -69,7 +78,7 @@ module Spree
69
78
  :meta_keywords, :price, :sku, :deleted_at, :prototype_id,
70
79
  :option_values_hash, :on_hand, :weight, :height, :width, :depth,
71
80
  :shipping_category_id, :tax_category_id, :product_properties_attributes,
72
- :variants_attributes, :taxon_ids
81
+ :variants_attributes, :taxon_ids, :option_type_ids
73
82
 
74
83
  attr_accessible :cost_price if Variant.table_exists? && Variant.column_names.include?('cost_price')
75
84
 
@@ -82,24 +91,23 @@ module Spree
82
91
 
83
92
  after_initialize :ensure_master
84
93
 
85
- def variants_with_only_master
86
- ActiveSupport::Deprecation.warn("[SPREE] Spree::Product#variants_with_only_master will be deprecated in Spree 1.3. Please use Spree::Product#master instead.")
87
- master
94
+ def ensure_master
95
+ return unless new_record?
96
+ self.master ||= Variant.new
88
97
  end
89
98
 
90
-
91
99
  def to_param
92
100
  permalink.present? ? permalink : (permalink_was || name.to_s.to_url)
93
101
  end
94
102
 
95
103
  # returns true if the product has any variants (the master variant is not a member of the variants array)
96
104
  def has_variants?
97
- variants.any?
105
+ variants.exists?
98
106
  end
99
107
 
100
108
  # returns the number of inventory units "on_hand" for this product
101
109
  def on_hand
102
- has_variants? ? variants.inject(0) { |sum, v| sum + v.on_hand } : master.on_hand
110
+ has_variants? ? variants.sum(&:on_hand) : master.on_hand
103
111
  end
104
112
 
105
113
  # adjusts the "on_hand" inventory level for the product up or down to match the given new_level
@@ -109,8 +117,9 @@ module Spree
109
117
  end
110
118
 
111
119
  # Returns true if there are inventory units (any variant) with "on_hand" state for this product
120
+ # Variants take precedence over master
112
121
  def has_stock?
113
- master.in_stock? || variants.any?(&:in_stock?)
122
+ has_variants? ? variants.any?(&:in_stock?) : master.in_stock?
114
123
  end
115
124
 
116
125
  def tax_category
@@ -121,13 +130,6 @@ module Spree
121
130
  end
122
131
  end
123
132
 
124
- # override the delete method to set deleted_at value
125
- # instead of actually deleting the product.
126
- def delete
127
- self.update_column(:deleted_at, Time.now)
128
- variants_including_master.update_all(:deleted_at => Time.now)
129
- end
130
-
131
133
  # Adding properties and option types on creation based on a chosen prototype
132
134
  attr_reader :prototype_id
133
135
  def prototype_id=(value)
@@ -202,14 +204,15 @@ module Spree
202
204
  end
203
205
 
204
206
  def set_property(property_name, property_value)
205
- prop = Spree::Property.find_or_initialize_by_name(property_name) do |p|
206
- p.presentation = property_name
207
- p.save!
207
+ ActiveRecord::Base.transaction do
208
+ property = Property.where(:name => property_name).first_or_initialize
209
+ property.presentation = property_name
210
+ property.save!
211
+
212
+ product_property = ProductProperty.where(:product_id => id, :property_id => property.id).first_or_initialize
213
+ product_property.value = property_value
214
+ product_property.save!
208
215
  end
209
-
210
- prod_prop = Spree::ProductProperty.find_or_initialize_by_product_id_and_property_id(self.id, prop.id)
211
- prod_prop.value = property_value
212
- prod_prop.save!
213
216
  end
214
217
 
215
218
  private
@@ -257,11 +260,6 @@ module Spree
257
260
  def save_master
258
261
  master.save if master && (master.changed? || master.new_record?)
259
262
  end
260
-
261
- def ensure_master
262
- return unless new_record?
263
- self.master ||= Variant.new
264
- end
265
263
  end
266
264
  end
267
265