SpreeConf 2012 (Day 1) training presentation. Gives an overview of Spree 1.0, followed by architecture discussions, installation details, and extension building
do # Load application's model / class decorators Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c| Rails.configuration.cache_classes ? require(c) : load(c) end end 82 Saturday, February 18, 12
:database_authenticatable, :token_authenticatable, :registerable, :recoverable, 4 :rememberable, :trackable, :validatable, :encryptable, :encryptor => 'authlogic_sha512' 5 6 has_many :orders 7 has_and_belongs_to_many :roles, :join_table => 'spree_roles_users' 8 belongs_to :ship_address, :foreign_key => 'ship_address_id', :class_name => 'Spree::Address' 9 belongs_to :bill_address, :foreign_key => 'bill_address_id', :class_name => 'Spree::Address' 10 11 before_save :check_admin 12 before_validation :set_login 13 14 # Setup accessible (or protected) attributes for your model 15 attr_accessible :email, :password, :password_confirmation, :remember_me, :persistence_token 16 17 users_table_name = User.table_name 18 roles_table_name = Role.table_name 19 20 scope :admin, lambda { includes(:roles).where("#{roles_table_name}.name" => "admin") } 21 scope :registered, where("#{users_table_name}.email NOT LIKE ?", "%@example.net") 22 23 # Creates an anonymous user. An anonymous user is basically an auto-generated +User+ account that is created for the customer 24 # behind the scenes and its completely transparently to the customer. All +Orders+ must have a +User+ so this is necessary 25 # when adding to the "cart" (which is really an order) and before the customer has a chance to provide an email or to register. 26 def self.anonymous! 27 token = User.generate_token(:persistence_token) 28 User.create(:email => "#{token}@example.net", :password => token, :password_confirmation => token, :persistence_token => token) 29 end 30 31 def anonymous? 32 email =~ /@example.net$/ 33 end spree/auth/app/models/spree/user.rb spree_auth ‘user’ 90 Saturday, February 18, 12
:associate_user spree/auth/app/controllers/spree/user_passwords_controller.rb spree/core/lib/spree/core/controller_helpers.rb 1 module Spree 2 module Core 3 module ControllerHelpers 4 .... 5 .... 6 def associate_user 7 return unless current_user and current_order 8 current_order.associate_user!(current_user) 9 session[:guest_token] = nil 10 end spree/auth/app/models/spree/order_decorator.rb Spree::Order.class_eval do token_resource # Associates the specified user with the order and destroys any previous association with guest user if # necessary. def associate_user!(user) self.user = user self.email = user.email # disable validations since this can cause issues when associating an incomplete address during the address step save(:validate => false) end end 97 Saturday, February 18, 12
5 can :create, Spree::Review do |review| 6 user.has_role?(:user) || !Spree::Reviews::Config[:require_login] 7 end 8 can :create, Spree::FeedbackReview do |review| 9 user.has_role?(:user) || !Spree::Reviews::Config[:require_login] 10 end 11 end 12 end Spree Reviews - Preferences app/models/spree/reviews_ability.rb 150 Saturday, February 18, 12
Rails::Generators::Base 4 5 def add_javascripts 6 append_file "app/assets/javascripts/store/all.js", "//= require store/jquery.rating\n" 7 end 8 9 def add_stylesheets 10 inject_into_file "app/assets/stylesheets/store/all.css", " *= require store/ spree_reviews\n", :before => /\*\//, :verbose => true 11 end 12 13 def add_migrations 14 run 'rake railties:install:migrations FROM=spree_reviews' 15 end 16 17 def run_migrations 18 res = ask "Would you like to run the migrations now? [Y/n]" 19 if res == "" || res.downcase == "y" 20 run 'rake db:migrate' 21 else 22 puts "Skipping rake db:migrate, don't forget to run it!" 23 end 24 end 25 end 26 end 27 end 161 Saturday, February 18, 12