Skip to content

Commit

Permalink
spree 1.0.0 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
lostboy committed Feb 18, 2012
1 parent 01af8ca commit b859a15
Show file tree
Hide file tree
Showing 31 changed files with 172 additions and 160 deletions.
93 changes: 0 additions & 93 deletions app/controllers/gift_cards_controller.rb

This file was deleted.

96 changes: 96 additions & 0 deletions app/controllers/spree/gift_cards_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
module Spree
class GiftCardsController < BaseController
helper 'spree/admin/base'
#before_filter :authenticate_user!, :only => :new
def new
find_gift_card_variants
@gift_card = GiftCard.new
#@gift_card.sender_name = [current_user.fname || "", " ", current_user.lname || ""].join
#@gift_card.sender_name = current_user.email
@gift_card.cc_me = true
end

def create
@gift_card = GiftCard.new(params[:gift_card])
if @gift_card.save
@order = current_order(true)
line_item = @order.add_variant(@gift_card.variant, 1)
@gift_card.line_item = line_item
@gift_card.sender = current_user
@gift_card.save
redirect_to cart_path
else
find_gift_card_variants
render :action => :new
end
end

def edit
@gift_card = GiftCard.find(params[:id])
access_forbidden unless @gift_card && @gift_card.sender == current_user
end

def update
@gift_card = GiftCard.find(params[:id])
access_forbidden unless @gift_card && @gift_card.sender == current_user && !@gift_card.is_received?
params[:gift_card].delete(:variant_id)
if @gift_card.update_attributes(params[:gift_card])
OrderMailer.gift_card_email(@gift_card, @gift_card.line_item.order).deliver if @gift_card.sent_at.present?
flash[:notice] = t("spree_gift_card.messages.successfully_updated")
redirect_to account_url
else
render :action => :edit
end
end

def activate
@gift_card = GiftCard.find_by_token(params[:id])
if @gift_card.is_received
flash[:error] = t("gift_card_messages.cant_activate")
redirect_to root_url
return
end

if current_user && !current_user.anonymous?
if @gift_card.register(current_user)
flash[:notice] = t("spree_gift_card.messages.activated")
else
flash[:error] = t("spree_gift_card.messages.register_error")
end
else
session[:gift_card] = @gift_card.token
flash[:notice] = t("spree_gift_card.messages.authorization_required")
end
redirect_to root_url
end

def preview
@gift_card = GiftCard.new(:email => params[:email], :name => params[:name], :sender_name => params[:sender_name], :variant_id => params[:variant_id])
end

def confirm
@gift_card = GiftCard.find_by_token(params[:id])
if @gift_card.is_received
flash[:error] = t("gift_card_messages.cant_activate")
redirect_to root_url
return
end

if !current_user || current_user.anonymous?
session[:gift_card] = @gift_card.token
flash[:notice] = t("spree_gift_card.messages.authorization_required")
redirect_to root_url
else
session[:gift_card] = nil
end

end

private

def find_gift_card_variants
gift_card_product_ids = Product.not_deleted.where(["is_gift_card = ?", true]).map(&:id)
@gift_card_variants = Variant.where(["price > 0 AND product_id IN (?)", gift_card_product_ids]).order("price")
end
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
UserSessionsController.class_eval do
Spree::UserSessionsController.class_eval do
after_filter :register_gift_card, :only => :create

private
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
UsersController.class_eval do
Spree::UsersController.class_eval do
after_filter :register_gift_card, :only => :create

private
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
OrderMailer.class_eval do
Spree::OrderMailer.class_eval do
def gift_card_email(card, order)
@gift_card = card
@order = order
Expand Down
2 changes: 1 addition & 1 deletion app/models/gift_card.rb → app/models/spree/gift_card.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class GiftCard < ActiveRecord::Base
class Spree::GiftCard < ActiveRecord::Base
belongs_to :variant
belongs_to :line_item
belongs_to :sender, :class_name => "User"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Order.class_eval do
Spree::Order.class_eval do
# Finalizes an in progress order after checkout is complete.
# Called after transition to complete state when payments will have been processed
def finalize_with_gift_card!
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Product.class_eval do
Spree::Product.class_eval do

scope :gift_cards, where(["products.is_gift_card = ?", true])
scope :not_gift_cards, where(["products.is_gift_card = ?", false])
Expand Down
6 changes: 6 additions & 0 deletions app/overrides/add_buy_gift_card_link.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Deface::Override.new(:virtual_path => "spree/layouts/spree_application",
:name => "converted_sidebar_164157054",
:insert_after => "[data-hook='sidebar'], #sidebar[data-hook]",
:text => "
<%= link_to t(\"spree_gift_card.buy_gift_card\"), new_gift_card_path, :class => 'button' %>
")
4 changes: 4 additions & 0 deletions app/overrides/add_gift_card_fields.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Deface::Override.new(:virtual_path => "spree/admin/products/_form",
:name => "converted_admin_product_form_right_637624031",
:insert_after => "#admin_product_form_right[data-hook], [data-hook='admin_product_form_right']",
:partial => "spree/admin/products/gift_card_fields")
5 changes: 5 additions & 0 deletions app/overrides/add_show_user_gift_cards.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Deface::Override.new(:virtual_path => "users/show",
:name => "converted_account_my_orders_408295644",
:insert_after => "[data-hook='account_my_orders'], #account_my_orders[data-hook]",
:partial => "users/gift_cards",
:disabled => false)
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1><%= t(:gift_card) %></h1>
<h3><%= t('spree_gift_card.instructions_resend')%></h3>
<%= render "shared/error_messages", :target => @gift_card %>
<%= render "spree/shared/error_messages", :target => @gift_card %>
<%= form_for @gift_card do |f| %>
<br/><p><strong>Amount: </strong><%= number_to_currency(@gift_card.price) %></p>
<%= f.field_container :sender_name do %>
Expand All @@ -23,6 +23,7 @@
<%= f.label :note, t("spree_gift_card.recipient_note") %><br />
<%= f.text_area :note, :rows => 10 %>
<% end %>

<button type='submit' class='large primary'>
<%= image_tag('/images/update.png') + ' ' + t('update') %>
</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<h3><%= t('spree_gift_card.instructions')%></h3>
<div>
<div style="float: left;">
<%= render "shared/error_messages", :target => @gift_card %>
<%= render "spree/shared/error_messages", :target => @gift_card %>
<%= form_for @gift_card do |f| %>
<p>
<%= f.label :variant_id, t("spree_gift_card.amount") %> <span class="required">*</span><br />
Expand Down Expand Up @@ -30,9 +30,9 @@
<%= f.label :note, t("spree_gift_card.recipient_note") %><br />
<%= f.text_area :note, :rows => 10 %>
<% end %>
<button type='submit' class='large primary'>
<%= image_tag('/images/add-to-cart.png') + ' ' + t('add_to_cart') %>
</button>
<%= button_tag :class => 'large primary', :id => 'add-to-cart-button' do %>
<%= t(:add_to_cart) %>
<% end %>
<% end %>
</div>
<div style="float: left; margin-left: 20px; display: none">
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Rails.application.routes.draw do
Spree::Core::Engine.routes.prepend do
resources :gift_cards do
get :activate, :on => :member
get :preview
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class AddGiftCardAttrToProducts < ActiveRecord::Migration
def self.up
change_table(:products) do |t|
change_table(:spree_products) do |t|
t.column :is_gift_card, :boolean, :default => false, :null => false
end
end

def self.down
change_table(:products) do |t|
change_table(:spree_products) do |t|
t.remove :is_gift_card
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class CreateGiftCards < ActiveRecord::Migration
def self.up
create_table :gift_cards do |t|
create_table :spree_gift_cards do |t|
t.integer :variant_id, :null => false
t.integer :line_item_id
t.integer :user_id
Expand All @@ -16,6 +16,6 @@ def self.up
end

def self.down
drop_table :gift_cards
drop_table :spree_gift_cards
end
end
15 changes: 15 additions & 0 deletions db/migrate/20111212100220_add_sender_name_to_gift_card.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class AddSenderNameToGiftCard < ActiveRecord::Migration
def self.up
add_column :spree_gift_cards, :sender_name, :string
add_column :spree_gift_cards, :recipient_id, :int
rename_column :spree_gift_cards, :user_id, :sender_id
add_column :spree_gift_cards, :store_credits_id, :int
end

def self.down
remove_column :spree_gift_cards, :sender_name
remove_column :spree_gift_cards, :recipient_id
rename_column :spree_gift_cards, :sender_id, :user_id
remove_column :spree_gift_cards, :store_credits_id
end
end
9 changes: 9 additions & 0 deletions db/migrate/20111212131520_fix_naming_issue_with_sc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class FixNamingIssueWithSc < ActiveRecord::Migration
def self.up
rename_column :spree_gift_cards, :store_credits_id, :store_credit_id
end

def self.down
rename_column :spree_gift_cards, :store_credit_id, :store_credit2_id
end
end
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class AddCcMeToGiftCards < ActiveRecord::Migration
def self.up
add_column :gift_cards, :cc_me, :boolean
add_column :spree_gift_cards, :cc_me, :boolean
end

def self.down
remove_column :gift_cards, :cc_me
remove_column :spree_gift_cards, :cc_me
end
end
10 changes: 5 additions & 5 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
if Product.where(:is_gift_card => true).count == 0
if Spree::Product.where(:is_gift_card => true).count == 0
puts "\tCreating default gift card..."
product = Product.new(:name => "Gift Card", :is_gift_card => true, :price => 0)
option_type = OptionType.new(:name => "is-gift-card", :presentation => "Gift Card")
option_type.option_values << OptionValue.new(:name => "true")
product = Spree::Product.new(:name => "Gift Card", :is_gift_card => true, :price => 0)
option_type = Spree::OptionType.new(:name => "is-gift-card", :presentation => "Gift Card")
option_type.option_values << Spree::OptionValue.new(:name => "true")
product.option_types << option_type
product.variants = [25, 50, 75, 100].map{|amount| Variant.new(:price => amount)}
product.variants = [25, 50, 75, 100].map{|amount| Spree::Variant.new(:price => amount)}
product.variants.map{|v| v.option_values << option_type.option_values}
product.save
end
15 changes: 10 additions & 5 deletions lib/generators/spree_gift_cards/install_generator.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
module SpreeGiftCards
module Generators
class InstallGenerator < Rails::Generators::Base
source_root File.expand_path("../../templates", __FILE__)

desc "Configures your Rails application for use with spree_gift_cards"
def add_migrations
run 'bundle exec rake railties:install:migrations FROM=spree_gift_cards'
end

def copy_migrations
directory "db"
def run_migrations
res = ask "Would you like to run the migrations now? [Y/n]"
if res == "" || res.downcase == "y"
run 'bundle exec rake db:migrate'
else
puts "Skiping rake db:migrate, don't forget to run it!"
end
end
end
end
Expand Down
Loading

0 comments on commit b859a15

Please sign in to comment.