Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
romul committed Oct 11, 2010
0 parents commit dab826a
Show file tree
Hide file tree
Showing 22 changed files with 330 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
\#*
*~
.#*
.DS_Store
.idea
.project
tmp
nbproject
*.swp
23 changes: 23 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the Rails Dog LLC nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
SpreeGiftCards
==============

Introduction goes here.


Example
=======

Example goes here.


Copyright (c) 2010 [name of extension creator], released under the New BSD License
29 changes: 29 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'rubygems'
require 'rake'
require 'rake/testtask'
require 'rake/packagetask'
require 'rake/gempackagetask'

spec = eval(File.read('spree_gift_cards.gemspec'))

Rake::GemPackageTask.new(spec) do |p|
p.gem_spec = spec
end

desc "Release to gemcutter"
task :release => :package do
require 'rake/gemcutter'
Rake::Gemcutter::Tasks.new(spec).define
Rake::Task['gem:push'].invoke
end

desc "Default Task"
task :default => [ :spec ]

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new

require 'cucumber/rake/task'
Cucumber::Rake::Task.new do |t|
t.cucumber_opts = %w{--format pretty}
end
27 changes: 27 additions & 0 deletions app/controllers/gift_cards_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class GiftCardsController < Spree::BaseController
helper 'admin/base'
def new
find_gift_card_variants
@gift_card = GiftCard.new
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.update_attributes(:line_item => line_item, :user => current_user)
redirect_to cart_path
else
find_gift_card_variants
render :action => :new
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
15 changes: 15 additions & 0 deletions app/models/gift_card.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class GiftCard < ActiveRecord::Base
belongs_to :variant
belongs_to :line_item
belongs_to :user
validates :email, :presence => true
validates :name, :presence => true

before_create :generate_token

private

def generate_token
self.token = Digest::SHA1.hexdigest([Time.now, rand].join)
end
end
3 changes: 3 additions & 0 deletions app/models/line_item_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
LineItem.class_eval do
has_one :gift_card
end
10 changes: 10 additions & 0 deletions app/models/product_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Product.class_eval do

scope :gift_cards, where(["products.is_gift_card = ?", true])
scope :not_gift_cards, where(["products.is_gift_card = ?", false])

scope :active, lambda { |*args|
not_deleted.not_gift_cards.available(args.first)
}

end
3 changes: 3 additions & 0 deletions app/views/admin/products/_gift_card_fields.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>
<%= f.check_box(:is_gift_card) %> <%= f.label :is_gift_card, t("is_gift_card")%>
</p>
24 changes: 24 additions & 0 deletions app/views/gift_cards/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<h1><%= t(:gift_cards) %></h1>
<%= render "shared/error_messages", :target => @gift_card %>
<%= form_for @gift_card do |f| %>
<p>
<% @gift_card_variants.each do |card| %>
<%= f.radio_button :variant_id, card.id %><%= number_to_currency(card.price) %>
<% end %>
</p>
<%= f.field_container :email do %>
<%= f.label :email, t("email") %> <span class="required">*</span><br />
<%= f.text_field :email %>
<% end %>
<%= f.field_container :name do %>
<%= f.label :name, t("name") %> <span class="required">*</span><br />
<%= f.text_field :name %>
<% end %>
<%= f.field_container :note do %>
<%= f.label :note, t("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>
<% end %>
11 changes: 11 additions & 0 deletions app/views/orders/_line_item_description.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<% if line_item.gift_card %>
<h4><%= t(:gift_card) %> <%= number_to_currency(variant.price) %> for
<%= line_item.gift_card.name %> (<%= line_item.gift_card.email %>)</h4>
<p>
<%= truncate(line_item.gift_card.note, :length => 100, :omission => "...") %>
</p>
<% else %>
<h4><%= link_to variant.product.name, product_path(variant.product) %></h4>
<%= variant_options variant %>
<%= truncate(variant.product.description, :length => 100, :omission => "...") %>
<% end %>
17 changes: 17 additions & 0 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
development:
adapter: sqlite3
database: db/cucumber.sqlite3
pool: 5
timeout: 5000

test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000

cucumber:
adapter: sqlite3
database: db/cucumber.sqlite3
pool: 5
timeout: 5000
38 changes: 38 additions & 0 deletions config/environments/cucumber.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
TestApp::Application.configure do
# Settings specified here will take precedence over those in config/environment.rb

# The test environment is used exclusively to run your application's
# test suite. You never need to work with it otherwise. Remember that
# your test database is "scratch space" for the test suite and is wiped
# and recreated between test runs. Don't rely on the data there!
config.cache_classes = true

# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true

# Show full error reports and disable caching
config.consider_all_requests_local = true
config.action_controller.perform_caching = false

# Raise exceptions instead of rendering exception templates
config.action_dispatch.show_exceptions = false

# Disable request forgery protection in test environment
config.action_controller.allow_forgery_protection = false

# Tell Action Mailer not to deliver emails to the real world.
# The :test delivery method accumulates sent emails in the
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test

# Use SQL instead of Active Record's schema dumper when creating the test database.
# This is necessary if your schema can't be completely dumped by the schema dumper,
# like if you have constraints or database-specific column types
# config.active_record.schema_format = :sql

# Print deprecation notices to the stderr
config.active_support.deprecation = :stderr

config.action_mailer.default_url_options = { :host => 'testapp.com' }

end
5 changes: 5 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
en:
buy_gift_card: "Buy gift card"
gift_card: "Gift card"
gift_cards: "Gift cards"
is_gift_card: "is gift card"
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Rails.application.routes.draw do
resources :gift_cards
end
15 changes: 15 additions & 0 deletions lib/generators/spree_gift_cards/install_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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 copy_migrations
directory "db"
end

end
end
end

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class AddGiftCardAttrToProducts < ActiveRecord::Migration
def self.up
change_table(:products) do |t|
t.column :is_gift_card, :boolean, :default => false, :null => false
end
end

def self.down
change_table(:products) do |t|
t.remove :is_gift_card
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class CreateGiftCards < ActiveRecord::Migration
def self.up
create_table :gift_cards do |t|
t.integer :variant_id, :null => false
t.integer :line_item_id
t.integer :user_id
t.string :email, :null => false
t.string :name
t.text :note
t.string :token, :null => false
t.boolean :is_received, :default => false, :null => false
t.datetime :sent_at
t.timestamps
end
end

def self.down
drop_table :gift_cards
end
end
17 changes: 17 additions & 0 deletions lib/spree_gift_cards.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'spree_core'
require 'spree_gift_cards_hooks'

module SpreeGiftCards
class Engine < Rails::Engine

config.autoload_paths += %W(#{config.root}/lib)

def self.activate
Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c|
Rails.env == "production" ? require(c) : load(c)
end
end

config.to_prepare &method(:activate).to_proc
end
end
11 changes: 11 additions & 0 deletions lib/spree_gift_cards_hooks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class SpreeGiftCardsHooks < Spree::ThemeSupport::HookListener
insert_after :admin_product_form_right , "admin/products/gift_card_fields"

insert_after :sidebar do
%(
<%= link_to t("buy_gift_card"), new_gift_card_path, :class => 'button' %>
)
end

replace :cart_item_description, "orders/line_item_description"
end
1 change: 1 addition & 0 deletions lib/tasks/spree_gift_cards.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# add custom rake tasks here
23 changes: 23 additions & 0 deletions spree-gift-cards.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.name = 'spree_gift_cards'
s.version = '0.30.0.beta2'
s.summary = 'Add gem summary here'
#s.description = 'Add (optional) gem description here'
s.required_ruby_version = '>= 1.8.7'

s.author = 'Roman Smirnov'
s.email = '[email protected]'
# s.homepage = 'http://www.rubyonrails.org'
# s.rubyforge_project = 'actionmailer'

s.files = Dir['CHANGELOG', 'README.md', 'LICENSE', 'lib/**/*', 'app/**/*']
s.require_path = 'lib'
s.requirements << 'none'

s.has_rdoc = true

s.add_dependency('spree_core', '>= 0.30.0.beta2')
s.add_dependency('spree_auth', '>= 0.30.0.beta2')
s.add_dependency('spree_store_credits', '>= 0.30.0.beta2')
end

0 comments on commit dab826a

Please sign in to comment.