From 6a27695dbd43fa319278140738c62a58c82e4675 Mon Sep 17 00:00:00 2001 From: Mumo Carlos Date: Mon, 12 Sep 2011 09:59:45 -0400 Subject: [PATCH 1/9] Make compatible with Spree 2.2 --- .rspec | 1 + Gemfile | 13 + Gemfile.lock | 311 ++++++++++++++++++ LICENSE | 19 +- README.md | 45 ++- Rakefile | 55 +--- Versionfile | 11 + .../spree/backend/spree_affiliate.js | 1 + .../spree/frontend/spree_affiliate.js | 1 + .../spree/backend/spree_affiliate.css | 3 + .../spree/frontend/spree_affiliate.css | 3 + .../checkout_controller_decorator.rb | 16 - .../email_sender_controller_decorator.rb | 10 - .../admin/affiliate_settings_controller.rb | 6 +- .../{ => spree}/affiliates_controller.rb | 2 +- ...user_registrations_controller_decorator.rb | 6 + .../spree/users_controller_decorator.rb | 11 + app/controllers/users_controller_decorator.rb | 35 -- app/helpers/spree/base_helper_decorator.rb | 11 + app/models/affiliate.rb | 13 - app/models/affiliate_event.rb | 5 - app/models/spree/affiliate.rb | 17 + app/models/spree/affiliate_event.rb | 10 + app/models/spree/order_decorator.rb | 14 + app/models/spree/user_decorator.rb | 24 ++ app/models/user_decorator.rb | 22 -- ..._admin_configuration_menu.html.haml.deface | 3 + .../show/affiliates_index.html.haml.deface | 4 + .../admin/affiliate_settings/edit.html.erb | 28 -- .../admin/affiliate_settings/show.html.erb | 26 -- app/views/affiliates/_mail_to_friend.html.erb | 13 - app/views/affiliates/show.html.erb | 0 app/views/email_sender/_fields.html.erb | 16 - .../admin/affiliate_settings/edit.html.haml | 25 ++ .../admin/affiliate_settings/show.html.haml | 27 ++ .../spree/affiliates/_affiliate.html.haml | 4 + .../spree/affiliates/_affiliates.html.haml | 17 + app/views/users/_affiliate.html.erb | 15 - bin/rails | 7 + config/database.yml | 17 - config/environments/cucumber.rb | 38 --- config/initializers/load_configuration.rb | 3 + config/locales/en.yml | 24 +- config/routes.rb | 4 +- .../20101022121217_create_affiliates.rb | 5 +- .../20101104145139_create_affiliate_events.rb | 4 +- lib/affiliate_credits.rb | 56 +++- .../install/install_generator.rb | 31 ++ lib/spree_affiliate.rb | 18 +- lib/spree_affiliate/configuration.rb | 8 + lib/spree_affiliate/engine.rb | 22 ++ lib/spree_affiliate/factories.rb | 6 + lib/spree_affiliate_hooks.rb | 9 - lib/tasks/install.rake | 16 - lib/tasks/spree_affiliate.rake | 1 - spec/models/user_spec.rb | 8 - spec/spec_helper.rb | 76 ++++- spree_affiliate.gemspec | 36 +- 58 files changed, 794 insertions(+), 438 deletions(-) create mode 100644 .rspec create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 Versionfile create mode 100644 app/assets/javascripts/spree/backend/spree_affiliate.js create mode 100644 app/assets/javascripts/spree/frontend/spree_affiliate.js create mode 100644 app/assets/stylesheets/spree/backend/spree_affiliate.css create mode 100644 app/assets/stylesheets/spree/frontend/spree_affiliate.css delete mode 100644 app/controllers/checkout_controller_decorator.rb delete mode 100644 app/controllers/email_sender_controller_decorator.rb rename app/controllers/{ => spree}/admin/affiliate_settings_controller.rb (51%) rename app/controllers/{ => spree}/affiliates_controller.rb (61%) create mode 100644 app/controllers/spree/user_registrations_controller_decorator.rb create mode 100644 app/controllers/spree/users_controller_decorator.rb delete mode 100644 app/controllers/users_controller_decorator.rb create mode 100644 app/helpers/spree/base_helper_decorator.rb delete mode 100644 app/models/affiliate.rb delete mode 100644 app/models/affiliate_event.rb create mode 100644 app/models/spree/affiliate.rb create mode 100644 app/models/spree/affiliate_event.rb create mode 100644 app/models/spree/order_decorator.rb create mode 100644 app/models/spree/user_decorator.rb delete mode 100644 app/models/user_decorator.rb create mode 100644 app/overrides/spree/admin/shared/_configuration_menu/affiliates_admin_configuration_menu.html.haml.deface create mode 100644 app/overrides/spree/users/show/affiliates_index.html.haml.deface delete mode 100644 app/views/admin/affiliate_settings/edit.html.erb delete mode 100644 app/views/admin/affiliate_settings/show.html.erb delete mode 100644 app/views/affiliates/_mail_to_friend.html.erb delete mode 100644 app/views/affiliates/show.html.erb delete mode 100644 app/views/email_sender/_fields.html.erb create mode 100644 app/views/spree/admin/affiliate_settings/edit.html.haml create mode 100644 app/views/spree/admin/affiliate_settings/show.html.haml create mode 100644 app/views/spree/affiliates/_affiliate.html.haml create mode 100644 app/views/spree/affiliates/_affiliates.html.haml delete mode 100644 app/views/users/_affiliate.html.erb create mode 100644 bin/rails delete mode 100644 config/database.yml delete mode 100644 config/environments/cucumber.rb create mode 100644 config/initializers/load_configuration.rb create mode 100644 lib/generators/spree_affiliate/install/install_generator.rb create mode 100644 lib/spree_affiliate/configuration.rb create mode 100644 lib/spree_affiliate/engine.rb create mode 100644 lib/spree_affiliate/factories.rb delete mode 100644 lib/spree_affiliate_hooks.rb delete mode 100644 lib/tasks/install.rake delete mode 100644 lib/tasks/spree_affiliate.rake delete mode 100644 spec/models/user_spec.rb diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..5052887 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--color \ No newline at end of file diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..efbf03e --- /dev/null +++ b/Gemfile @@ -0,0 +1,13 @@ +source 'https://rubygems.org' + +gem 'haml-rails' + +gem 'spree_auth_devise', + github: 'spree/spree_auth_devise', + branch: '2-2-stable' + +gem 'spree_store_credits', + github: 'spree/spree_store_credits', + branch: '2-2-stable' + +gemspec diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..014a9e5 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,311 @@ +GIT + remote: git://github.com/spree/spree_auth_devise.git + revision: f901dc03fa95a0594fda63b7383a49164270bb48 + branch: 2-2-stable + specs: + spree_auth_devise (2.2.0) + cancan (~> 1.6.10) + devise (~> 3.2.3) + devise-encryptable (= 0.1.2) + json + multi_json + spree_core (~> 2.2.0) + +GIT + remote: git://github.com/spree/spree_store_credits.git + revision: d7629b066c7acfb5ab36d5598e4201f3e1d85201 + branch: 2-2-stable + specs: + spree_store_credits (1.1.1) + spree_api (~> 2.2.0.beta) + spree_backend (~> 2.2.0.beta) + spree_core (~> 2.2.0.beta) + spree_frontend (~> 2.2.0.beta) + +PATH + remote: . + specs: + spree_affiliate (2.2.1) + spree_core (~> 2.2.1) + +GEM + remote: https://rubygems.org/ + specs: + actionmailer (4.0.5) + actionpack (= 4.0.5) + mail (~> 2.5.4) + actionpack (4.0.5) + activesupport (= 4.0.5) + builder (~> 3.1.0) + erubis (~> 2.7.0) + rack (~> 1.5.2) + rack-test (~> 0.6.2) + active_utils (2.0.2) + activesupport (>= 2.3.11) + i18n + activemerchant (1.42.9) + active_utils (~> 2.0, >= 2.0.1) + activesupport (>= 2.3.14, < 5.0.0) + builder (>= 2.1.2, < 4.0.0) + i18n (~> 0.5) + json (~> 1.7) + money (< 7.0.0) + nokogiri (~> 1.4) + activemodel (4.0.5) + activesupport (= 4.0.5) + builder (~> 3.1.0) + activerecord (4.0.5) + activemodel (= 4.0.5) + activerecord-deprecated_finders (~> 1.0.2) + activesupport (= 4.0.5) + arel (~> 4.0.0) + activerecord-deprecated_finders (1.0.3) + activesupport (4.0.5) + i18n (~> 0.6, >= 0.6.9) + minitest (~> 4.2) + multi_json (~> 1.3) + thread_safe (~> 0.1) + tzinfo (~> 0.3.37) + acts_as_list (0.3.0) + activerecord (>= 3.0) + arel (4.0.2) + awesome_nested_set (3.0.0.rc.4) + activerecord (>= 4.0.0, < 5) + aws-sdk (1.27.0) + json (~> 1.4) + nokogiri (>= 1.4.4) + uuidtools (~> 2.1) + bcrypt (3.1.7) + builder (3.1.4) + cancan (1.6.10) + canonical-rails (0.0.7) + rails (>= 3.1, < 5.0) + capybara (2.2.1) + mime-types (>= 1.16) + nokogiri (>= 1.3.3) + rack (>= 1.0.0) + rack-test (>= 0.5.4) + xpath (~> 2.0) + childprocess (0.5.3) + ffi (~> 1.0, >= 1.0.11) + climate_control (0.0.3) + activesupport (>= 3.0) + cocaine (0.5.4) + climate_control (>= 0.0.3, < 1.0) + coffee-rails (4.0.1) + coffee-script (>= 2.2.0) + railties (>= 4.0.0, < 5.0) + coffee-script (2.2.0) + coffee-script-source + execjs + coffee-script-source (1.7.0) + colorize (0.7.2) + database_cleaner (1.2.0) + deface (1.0.0) + colorize (>= 0.5.8) + nokogiri (~> 1.6.0) + rails (>= 3.1) + devise (3.2.4) + bcrypt (~> 3.0) + orm_adapter (~> 0.1) + railties (>= 3.2.6, < 5) + thread_safe (~> 0.1) + warden (~> 1.2.3) + devise-encryptable (0.1.2) + devise (>= 2.1.0) + diff-lcs (1.2.5) + docile (1.1.3) + erubis (2.7.0) + execjs (2.0.2) + factory_girl (4.4.0) + activesupport (>= 3.0.0) + ffaker (1.24.0) + ffi (1.9.3) + friendly_id (5.0.3) + activerecord (>= 4.0.0) + haml (4.0.5) + tilt + haml-rails (0.5.3) + actionpack (>= 4.0.1) + activesupport (>= 4.0.1) + haml (>= 3.1, < 5.0) + railties (>= 4.0.1) + highline (1.6.21) + hike (1.2.3) + httparty (0.13.1) + json (~> 1.8) + multi_xml (>= 0.5.2) + i18n (0.6.9) + jquery-rails (3.1.0) + railties (>= 3.0, < 5.0) + thor (>= 0.14, < 2.0) + jquery-ui-rails (4.1.2) + railties (>= 3.1.0) + json (1.8.1) + kaminari (0.15.1) + actionpack (>= 3.0.0) + activesupport (>= 3.0.0) + mail (2.5.4) + mime-types (~> 1.16) + treetop (~> 1.4.8) + mime-types (1.25.1) + mini_portile (0.5.3) + minitest (4.7.5) + money (6.1.1) + i18n (~> 0.6.4) + multi_json (1.10.0) + multi_xml (0.5.5) + nokogiri (1.6.1) + mini_portile (~> 0.5.0) + orm_adapter (0.5.0) + paperclip (3.4.2) + activemodel (>= 3.0.0) + activerecord (>= 3.0.0) + activesupport (>= 3.0.0) + cocaine (~> 0.5.0) + mime-types + paranoia (2.0.2) + activerecord (~> 4.0) + polyamorous (0.6.4) + activerecord (>= 3.0) + polyglot (0.3.4) + rabl (0.9.3) + activesupport (>= 2.3.14) + rack (1.5.2) + rack-test (0.6.2) + rack (>= 1.0) + rails (4.0.5) + actionmailer (= 4.0.5) + actionpack (= 4.0.5) + activerecord (= 4.0.5) + activesupport (= 4.0.5) + bundler (>= 1.3.0, < 2.0) + railties (= 4.0.5) + sprockets-rails (~> 2.0.0) + railties (4.0.5) + actionpack (= 4.0.5) + activesupport (= 4.0.5) + rake (>= 0.8.7) + thor (>= 0.18.1, < 2.0) + rake (10.3.1) + ransack (1.1.0) + actionpack (>= 3.0) + activerecord (>= 3.0) + polyamorous (~> 0.6.0) + rspec-core (2.14.8) + rspec-expectations (2.14.5) + diff-lcs (>= 1.1.3, < 2.0) + rspec-mocks (2.14.6) + rspec-rails (2.14.2) + actionpack (>= 3.0) + activemodel (>= 3.0) + activesupport (>= 3.0) + railties (>= 3.0) + rspec-core (~> 2.14.0) + rspec-expectations (~> 2.14.0) + rspec-mocks (~> 2.14.0) + rubyzip (1.1.3) + sass (3.3.7) + sass-rails (4.0.1) + railties (>= 4.0.0, < 5.0) + sass (>= 3.1.10) + sprockets-rails (~> 2.0.0) + select2-rails (3.4.9) + sass-rails + thor (~> 0.14) + selenium-webdriver (2.41.0) + childprocess (>= 0.5.0) + multi_json (~> 1.0) + rubyzip (~> 1.0) + websocket (~> 1.0.4) + simplecov (0.8.2) + docile (~> 1.1.0) + multi_json + simplecov-html (~> 0.8.0) + simplecov-html (0.8.0) + spree_api (2.2.1) + rabl (= 0.9.3) + spree_core (= 2.2.1) + versioncake (~> 1.2.0) + spree_backend (2.2.1) + jquery-rails (~> 3.1.0) + jquery-ui-rails (~> 4.1.0) + select2-rails (~> 3.4.7) + spree_api (= 2.2.1) + spree_core (= 2.2.1) + spree_core (2.2.1) + activemerchant (~> 1.42.3) + acts_as_list (= 0.3.0) + awesome_nested_set (~> 3.0.0.rc.3) + aws-sdk (= 1.27.0) + cancan (~> 1.6.10) + deface (~> 1.0.0) + ffaker (~> 1.16) + friendly_id (= 5.0.3) + highline (~> 1.6.18) + httparty (~> 0.11) + json (~> 1.7) + kaminari (~> 0.15.0) + paperclip (~> 3.4.1) + paranoia (~> 2.0) + rails (~> 4.0.3) + ransack (~> 1.1.0) + state_machine (= 1.2.0) + stringex (~> 1.5.1) + truncate_html (= 0.9.2) + spree_frontend (2.2.1) + canonical-rails (~> 0.0.4) + jquery-rails (~> 3.1.0) + spree_api (= 2.2.1) + spree_core (= 2.2.1) + stringex (~> 1.5.1) + sprockets (2.12.1) + hike (~> 1.2) + multi_json (~> 1.0) + rack (~> 1.0) + tilt (~> 1.1, != 1.3.0) + sprockets-rails (2.0.1) + actionpack (>= 3.0) + activesupport (>= 3.0) + sprockets (~> 2.8) + sqlite3 (1.3.9) + state_machine (1.2.0) + stringex (1.5.1) + thor (0.19.1) + thread_safe (0.3.3) + tilt (1.4.1) + treetop (1.4.15) + polyglot + polyglot (>= 0.3.1) + truncate_html (0.9.2) + tzinfo (0.3.39) + uuidtools (2.1.4) + versioncake (1.2.0) + actionpack (>= 3.2) + activesupport (>= 3.2) + railties (>= 3.2) + tzinfo + warden (1.2.3) + rack (>= 1.0) + websocket (1.0.7) + xpath (2.0.0) + nokogiri (~> 1.3) + +PLATFORMS + ruby + +DEPENDENCIES + capybara (~> 2.1) + coffee-rails + database_cleaner + factory_girl (~> 4.4) + ffaker + haml-rails + rspec-rails (~> 2.13) + sass-rails + selenium-webdriver + simplecov + spree_affiliate! + spree_auth_devise! + spree_store_credits! + sqlite3 diff --git a/LICENSE b/LICENSE index 5800638..9822124 100644 --- a/LICENSE +++ b/LICENSE @@ -1,14 +1,17 @@ -Redistribution and use in source and binary forms, with or without modification, +Copyright (c) 2013 [name of plugin creator] +All rights reserved. + +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, + * 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 + * 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. + * Neither the name Spree 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 @@ -20,4 +23,4 @@ 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. \ No newline at end of file +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md index 0aaa403..e03bcca 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,40 @@ -Spree Affiliate -=============== -Allows customers to refer friends and earn store credit for each user who registers and/or orders. +SpreeAffiliate +============== -You can also give store credit to the refered friends on signup and after first order. +Introduction goes here. Installation -============ +------------ -1. Add to Gemfile: +Add spree_affiliate to your Gemfile: - gem "spree_store_credits" - gem "spree_email_to_friend" - gem "spree_affiliate" +```ruby +gem 'spree_affiliate' +``` -1. Run `bundle install` -1. Run install rake task for all extensions: +Bundle your dependencies and run the installation generator: - rake spree_store_credits:install - rake spree_affiliate:install +```shell +bundle +bundle exec rails g spree_affiliate:install +``` -1. Run `rake db:migrate` +Testing +------- +Be sure to bundle your dependencies and then create a dummy test app for the specs to run against. + +```shell +bundle +bundle exec rake test_app +bundle exec rspec spec +``` + +When testing your applications integration with this extension you may use it's factories. +Simply add this require statement to your spec_helper: + +```ruby +require 'spree_affiliate/factories' +``` + +Copyright (c) 2013 [name of extension creator], released under the New BSD License diff --git a/Rakefile b/Rakefile index ee53582..7a977e6 100644 --- a/Rakefile +++ b/Rakefile @@ -1,54 +1,15 @@ -# encoding: utf-8 -require 'rubygems' -require 'rake' -require 'rake/testtask' - -desc "Default Task" -task :default => [ :spec ] +require 'bundler' +Bundler::GemHelper.install_tasks require 'rspec/core/rake_task' +require 'spree/testing_support/extension_rake' + RSpec::Core::RakeTask.new -require 'cucumber/rake/task' -Cucumber::Rake::Task.new do |t| - t.cucumber_opts = %w{--format pretty} -end +task :default => [:spec] -desc "Regenerates a rails 3 app for testing" +desc 'Generates a dummy app for testing' task :test_app do - SPREE_PATH = ENV['SPREE_PATH'] - raise "SPREE_PATH should be specified" unless SPREE_PATH - require File.join(SPREE_PATH, 'lib/generators/spree/test_app_generator') - class AuthTestAppGenerator < Spree::Generators::TestAppGenerator - def tweak_gemfile - append_file 'Gemfile' do -<<-gems -gem 'spree_core', :path => '#{File.join(SPREE_PATH, 'core')}' -gem 'spree_auth', :path => '#{File.join(SPREE_PATH, 'auth')}' -gem 'spree_store_credits', :path => '#{File.join(SPREE_PATH, '..', 'spree_store_credits')}' -gem 'spree_email_to_friend', :path => '#{File.join(SPREE_PATH, '..', 'spree-email-to-friend')}' -gem 'spree_affiliate', :path => '#{File.dirname(__FILE__)}' -gems - end - end - - def install_gems - system("cd spec/test_app && rake spree_core:install") - system("cd spec/test_app && rake spree_auth:install") - generate 'spree_store_credits:install -f' - generate 'spree_affiliate:install -f' - end - - def migrate_db - run_migrations - end - end - AuthTestAppGenerator.start -end - -namespace :test_app do - desc 'Rebuild test and cucumber databases' - task :rebuild_dbs do - system("cd spec/test_app && rake db:drop db:migrate RAILS_ENV=test && rake db:drop db:migrate RAILS_ENV=cucumber") - end + ENV['LIB_NAME'] = 'spree_affiliate' + Rake::Task['extension:test_app'].invoke end diff --git a/Versionfile b/Versionfile new file mode 100644 index 0000000..5db83aa --- /dev/null +++ b/Versionfile @@ -0,0 +1,11 @@ +# This file is used to designate compatibilty with different versions of Spree +# Please see http://spreecommerce.com/documentation/extensions.html#versionfile for details + +# Examples +# +# '1.2.x' => { :branch => 'master' } +# '1.1.x' => { :branch => '1-1-stable' } +# '1.0.x' => { :branch => '1-0-stable' } +# '0.70.x' => { :branch => '0-70-stable' } +# '0.40.x' => { :tag => 'v1.0.0', :version => '1.0.0' } + diff --git a/app/assets/javascripts/spree/backend/spree_affiliate.js b/app/assets/javascripts/spree/backend/spree_affiliate.js new file mode 100644 index 0000000..1424ae2 --- /dev/null +++ b/app/assets/javascripts/spree/backend/spree_affiliate.js @@ -0,0 +1 @@ +//= require spree/backend diff --git a/app/assets/javascripts/spree/frontend/spree_affiliate.js b/app/assets/javascripts/spree/frontend/spree_affiliate.js new file mode 100644 index 0000000..fa0a16e --- /dev/null +++ b/app/assets/javascripts/spree/frontend/spree_affiliate.js @@ -0,0 +1 @@ +//= require spree/frontend diff --git a/app/assets/stylesheets/spree/backend/spree_affiliate.css b/app/assets/stylesheets/spree/backend/spree_affiliate.css new file mode 100644 index 0000000..a5cec98 --- /dev/null +++ b/app/assets/stylesheets/spree/backend/spree_affiliate.css @@ -0,0 +1,3 @@ +/* + *= require spree/backend +*/ diff --git a/app/assets/stylesheets/spree/frontend/spree_affiliate.css b/app/assets/stylesheets/spree/frontend/spree_affiliate.css new file mode 100644 index 0000000..ad7db30 --- /dev/null +++ b/app/assets/stylesheets/spree/frontend/spree_affiliate.css @@ -0,0 +1,3 @@ +/* + *= require spree/frontend +*/ diff --git a/app/controllers/checkout_controller_decorator.rb b/app/controllers/checkout_controller_decorator.rb deleted file mode 100644 index 9b80c07..0000000 --- a/app/controllers/checkout_controller_decorator.rb +++ /dev/null @@ -1,16 +0,0 @@ -CheckoutController.class_eval do - include AffiliateCredits - - private - - def after_complete - session[:order_id] = nil - - if current_user && current_user.affiliate_partner && current_user.orders.where(:state => 'complete').count == 1 - sender = current_user.partner - - #create credit (if required) - create_affiliate_credits(sender, current_user, "purchase") - end - end -end diff --git a/app/controllers/email_sender_controller_decorator.rb b/app/controllers/email_sender_controller_decorator.rb deleted file mode 100644 index 375b05d..0000000 --- a/app/controllers/email_sender_controller_decorator.rb +++ /dev/null @@ -1,10 +0,0 @@ -EmailSenderController.class_eval do - private - - def after_delivering_affiliate_mail - emails = @mail_to_friend.recipient_email.split(/,\s?/) - emails.each do |email| - current_user.affiliates.create(:affiliate_email => email) if Affiliate.where(:affiliate_email => email).count == 0 - end - end -end diff --git a/app/controllers/admin/affiliate_settings_controller.rb b/app/controllers/spree/admin/affiliate_settings_controller.rb similarity index 51% rename from app/controllers/admin/affiliate_settings_controller.rb rename to app/controllers/spree/admin/affiliate_settings_controller.rb index 583a503..caf1e4c 100644 --- a/app/controllers/admin/affiliate_settings_controller.rb +++ b/app/controllers/spree/admin/affiliate_settings_controller.rb @@ -1,8 +1,8 @@ -class Admin::AffiliateSettingsController < Admin::BaseController +class Spree::Admin::AffiliateSettingsController < Spree::Admin::BaseController def update - Spree::Config.set(params[:preferences]) - + SpreeAffiliate::Config.set(params[:preferences]) + respond_to do |format| format.html { redirect_to admin_affiliate_settings_path diff --git a/app/controllers/affiliates_controller.rb b/app/controllers/spree/affiliates_controller.rb similarity index 61% rename from app/controllers/affiliates_controller.rb rename to app/controllers/spree/affiliates_controller.rb index 5394098..fb955e7 100644 --- a/app/controllers/affiliates_controller.rb +++ b/app/controllers/spree/affiliates_controller.rb @@ -1,4 +1,4 @@ -class AffiliatesController < Spree::BaseController +class Spree::AffiliatesController < Spree::BaseController def show flash[:notice] = request.flash[:notice] redirect_to account_url diff --git a/app/controllers/spree/user_registrations_controller_decorator.rb b/app/controllers/spree/user_registrations_controller_decorator.rb new file mode 100644 index 0000000..16d1cb6 --- /dev/null +++ b/app/controllers/spree/user_registrations_controller_decorator.rb @@ -0,0 +1,6 @@ +Spree::UserRegistrationsController.class_eval do + include AffiliateCredits + + after_action :check_affiliate, only: :create +end + diff --git a/app/controllers/spree/users_controller_decorator.rb b/app/controllers/spree/users_controller_decorator.rb new file mode 100644 index 0000000..6d79df0 --- /dev/null +++ b/app/controllers/spree/users_controller_decorator.rb @@ -0,0 +1,11 @@ +Spree::UsersController.class_eval do + before_action :find_affiliates, only: :show + + private + + def find_affiliates + @affiliates ||= current_spree_user.affiliates + end + +end + diff --git a/app/controllers/users_controller_decorator.rb b/app/controllers/users_controller_decorator.rb deleted file mode 100644 index b67c3f4..0000000 --- a/app/controllers/users_controller_decorator.rb +++ /dev/null @@ -1,35 +0,0 @@ -UserRegistrationsController.class_eval do - include AffiliateCredits - - after_filter :check_affiliate, :only => :create - - private - - def check_affiliate - return if cookies[:ref_id].blank? or @user.invalid? - sender = User.find_by_ref_id(cookies[:ref_id]) - - if sender - #see if email address used is an invited affiliate - affiliate = sender.affiliates.where(:affiliate_email => @user.email).first - - #if none exist create an affiliate - if affiliate.nil? && Affiliate.where(:affiliate_email => @user.email).count == 0 - affiliate = sender.affiliates.create(:affiliate_email => @user.email) - else - #affiliate is registered to a different account (other than the ref_id above). what should we do? - end - - #link affiliate record with newly registered user - if affiliate and affiliate.user.nil? - affiliate.update_attribute(:user_id, @user.id) - end - - #create credit (if required) - create_affiliate_credits(sender, @user, "register") - end - - #destroy the cookie, as the affiliate record has been created. - cookies[:ref_id] = nil - end -end diff --git a/app/helpers/spree/base_helper_decorator.rb b/app/helpers/spree/base_helper_decorator.rb new file mode 100644 index 0000000..84130ec --- /dev/null +++ b/app/helpers/spree/base_helper_decorator.rb @@ -0,0 +1,11 @@ +Spree::BaseHelper.class_eval do + def referral_url(referrer) + root_url(ref_id: referrer.ref_id) + end + + def link_to_referral_url(referrer, options = {}) + url = referral_url(referrer) + + link_to(url, url, options) + end +end diff --git a/app/models/affiliate.rb b/app/models/affiliate.rb deleted file mode 100644 index be33a2a..0000000 --- a/app/models/affiliate.rb +++ /dev/null @@ -1,13 +0,0 @@ -class Affiliate < ActiveRecord::Base - belongs_to :partner, :class_name => "User", :foreign_key => :partner_id - belongs_to :user - has_many :events, :class_name => "AffiliateEvent" - - def name - I18n.t(:affiliate_program) - end - - def ref_id - partner ? partner.ref_id : '' - end -end diff --git a/app/models/affiliate_event.rb b/app/models/affiliate_event.rb deleted file mode 100644 index 93839ec..0000000 --- a/app/models/affiliate_event.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AffiliateEvent < ActiveRecord::Base - belongs_to :affiliate - belongs_to :reward, :polymorphic => true - belongs_to :user -end diff --git a/app/models/spree/affiliate.rb b/app/models/spree/affiliate.rb new file mode 100644 index 0000000..35837f2 --- /dev/null +++ b/app/models/spree/affiliate.rb @@ -0,0 +1,17 @@ +class Spree::Affiliate < ActiveRecord::Base + belongs_to :partner, + class_name: 'Spree::User', + foreign_key: :partner_id + belongs_to :user, + class_name: 'Spree::User' + has_many :events, + class_name: 'Spree::AffiliateEvent' + + def name + I18n.t(:affiliate_program) + end + + def ref_id + partner.try(:ref_id) || '' + end +end diff --git a/app/models/spree/affiliate_event.rb b/app/models/spree/affiliate_event.rb new file mode 100644 index 0000000..aa371ad --- /dev/null +++ b/app/models/spree/affiliate_event.rb @@ -0,0 +1,10 @@ +class Spree::AffiliateEvent < ActiveRecord::Base + belongs_to :affiliate, + class_name: 'Spree::Affiliate' + belongs_to :reward, + polymorphic: true + belongs_to :user, + class_name: 'Spree::User' + +end + diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb new file mode 100644 index 0000000..a673387 --- /dev/null +++ b/app/models/spree/order_decorator.rb @@ -0,0 +1,14 @@ +Spree::Order.class_eval do + include AffiliateCredits + + alias :old_finalize! :finalize! + + def finalize! + old_finalize! + if user && user.affiliate_partner && user.orders.where(state: 'complete').count == 1 + sender = user.referred_by + create_affiliate_credits(sender, user, 'purchase') + end + end + +end diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb new file mode 100644 index 0000000..ebb880f --- /dev/null +++ b/app/models/spree/user_decorator.rb @@ -0,0 +1,24 @@ +Spree::User.class_eval do + + has_many :affiliates, + class_name: 'Spree::Affiliate', + foreign_key: 'partner_id' + has_many :referred_users, + class_name: 'Spree::User', + through: :affiliates + has_one :affiliate_partner, + class_name:'Spree::Affiliate', + foreign_key: 'user_id' + + def referred_by + affiliate_partner.partner + end + + def ref_id + self.id.to_s.reverse + end + + def self.find_by_ref_id(ref_id) + Spree::User.find(ref_id.to_s.reverse) + end +end diff --git a/app/models/user_decorator.rb b/app/models/user_decorator.rb deleted file mode 100644 index fa57384..0000000 --- a/app/models/user_decorator.rb +++ /dev/null @@ -1,22 +0,0 @@ -User.class_eval do - - has_many :affiliates, :foreign_key => "partner_id" - has_one :affiliate_partner, :class_name => "Affiliate", :foreign_key => "user_id" - - def partner - affiliate_partner.partner - end - - def ref_id - self.id.to_s.reverse - end - - def find_or_create_affiliate - Affiliate.where(:partner_id => self.id, :affiliate_email => nil).first || - Affiliate.create(:partner => self) - end - - def self.find_by_ref_id(ref_id) - User.find(ref_id.to_s.reverse) - end -end diff --git a/app/overrides/spree/admin/shared/_configuration_menu/affiliates_admin_configuration_menu.html.haml.deface b/app/overrides/spree/admin/shared/_configuration_menu/affiliates_admin_configuration_menu.html.haml.deface new file mode 100644 index 0000000..3831ece --- /dev/null +++ b/app/overrides/spree/admin/shared/_configuration_menu/affiliates_admin_configuration_menu.html.haml.deface @@ -0,0 +1,3 @@ +/ insert_bottom "[data-hook='admin_configurations_sidebar_menu']" + += configurations_sidebar_menu_item t(:affiliate_settings), spree.admin_affiliate_settings_path diff --git a/app/overrides/spree/users/show/affiliates_index.html.haml.deface b/app/overrides/spree/users/show/affiliates_index.html.haml.deface new file mode 100644 index 0000000..6c86eb7 --- /dev/null +++ b/app/overrides/spree/users/show/affiliates_index.html.haml.deface @@ -0,0 +1,4 @@ +/ insert_after "[data-hook='account_my_orders'], #account_my_orders[data-hook]" + += render 'spree/affiliates/affiliates' + diff --git a/app/views/admin/affiliate_settings/edit.html.erb b/app/views/admin/affiliate_settings/edit.html.erb deleted file mode 100644 index 77bbc61..0000000 --- a/app/views/admin/affiliate_settings/edit.html.erb +++ /dev/null @@ -1,28 +0,0 @@ -<%= render :partial => 'admin/shared/configuration_menu' %> - -

<%= t('affiliate_settings') %>

- -<%= form_tag(admin_affiliate_settings_path, :method => :put) do %> - -

- - <%= text_field_tag('preferences[sender_credit_on_register_amount]', Spree::Config[:sender_credit_on_register_amount]) %> -

-

- - <%= text_field_tag('preferences[recipient_credit_on_register_amount]', Spree::Config[:recipient_credit_on_register_amount])%> -

-

- - <%= text_field_tag('preferences[sender_credit_on_purchase_amount]', Spree::Config[:sender_credit_on_purchase_amount]) %> -

-

- - <%= text_field_tag('preferences[recipient_credit_on_purchase_amount]', Spree::Config[:recipient_credit_on_purchase_amount])%> -

- -

- <%= button t('update') %> - <%= t("or") %> <%= link_to t("cancel"), admin_affiliate_settings_url %> -

-<% end %> diff --git a/app/views/admin/affiliate_settings/show.html.erb b/app/views/admin/affiliate_settings/show.html.erb deleted file mode 100644 index 202380d..0000000 --- a/app/views/admin/affiliate_settings/show.html.erb +++ /dev/null @@ -1,26 +0,0 @@ -<%= render :partial => 'admin/shared/configuration_menu' %> - -

<%= t("affiliate_settings") %>

- - - - - - - - - - - - - - - - - - -
<%= t("sender_credit_on_register_amount") %>:<%= number_to_currency Spree::Config[:sender_credit_on_register_amount] %>
<%= t("recipient_credit_on_register_amount") %>:<%= number_to_currency Spree::Config[:recipient_credit_on_register_amount] %>
<%= t("sender_credit_on_purchase_amount") %>:<%= number_to_currency Spree::Config[:sender_credit_on_purchase_amount] %>
<%= t("recipient_credit_on_purchase_amount") %>:<%= number_to_currency Spree::Config[:recipient_credit_on_purchase_amount] %>
- -

<%= link_to_with_icon 'edit', t("edit"), edit_admin_affiliate_settings_path %>

- - diff --git a/app/views/affiliates/_mail_to_friend.html.erb b/app/views/affiliates/_mail_to_friend.html.erb deleted file mode 100644 index 04d204c..0000000 --- a/app/views/affiliates/_mail_to_friend.html.erb +++ /dev/null @@ -1,13 +0,0 @@ -<%= @mail.recipient_name %>, - -I think you would like this affiliate program. - -<% unless @mail.message.blank? -%> -<%= @mail.message %> -<% end -%> - -<%= @mail.sender_name %> - -Click the link below to to get store credit after first order: - -<%= link_to root_url(:ref_id => @object.ref_id), root_url(:ref_id => @object.ref_id) %> diff --git a/app/views/affiliates/show.html.erb b/app/views/affiliates/show.html.erb deleted file mode 100644 index e69de29..0000000 diff --git a/app/views/email_sender/_fields.html.erb b/app/views/email_sender/_fields.html.erb deleted file mode 100644 index 4631416..0000000 --- a/app/views/email_sender/_fields.html.erb +++ /dev/null @@ -1,16 +0,0 @@ -

- <%= f.label(:sender_name, t('email_to_friend.sender_name')) %>
- <%= f.text_field(:sender_name) %> -

-

- <%= f.label(:sender_email, t('email_to_friend.sender_email')) %>
- <%= f.text_field(:sender_email) %> -

-

- <%= f.label(:recipient_email, t('email_to_friend.recipient_emails')) %>
- <%= f.text_field(:recipient_email) %> -

-

- <%= f.label(:message, t('email_to_friend.message')) %>
- <%= f.text_area(:message) %> -

diff --git a/app/views/spree/admin/affiliate_settings/edit.html.haml b/app/views/spree/admin/affiliate_settings/edit.html.haml new file mode 100644 index 0000000..4df11bf --- /dev/null +++ b/app/views/spree/admin/affiliate_settings/edit.html.haml @@ -0,0 +1,25 @@ += render 'spree/admin/shared/configuration_menu' + +%h1= t('affiliate_settings') + += form_tag admin_affiliate_settings_path, method: :put do + %p + %label= Spree.t('sender_credit_on_register_amount') + = text_field_tag('preferences[sender_credit_on_register_amount]', + SpreeAffiliate::Config[:sender_credit_on_register_amount]) + %p + %label= Spree.t('recipient_credit_on_register_amount') + = text_field_tag('preferences[recipient_credit_on_register_amount]', + SpreeAffiliate::Config[:recipient_credit_on_register_amount]) + %p + %label= Spree.t('sender_credit_on_purchase_amount') + = text_field_tag('preferences[sender_credit_on_purchase_amount]', + SpreeAffiliate::Config[:sender_credit_on_purchase_amount]) + %p + %label= Spree.t('recipient_credit_on_purchase_amount') + = text_field_tag('preferences[recipient_credit_on_purchase_amount]', + SpreeAffiliate::Config[:recipient_credit_on_purchase_amount]) + + %p.form-buttons + = button Spree.t('actions.update') + = link_to Spree.t('actions.cancel'), admin_affiliate_settings_url diff --git a/app/views/spree/admin/affiliate_settings/show.html.haml b/app/views/spree/admin/affiliate_settings/show.html.haml new file mode 100644 index 0000000..b1496f2 --- /dev/null +++ b/app/views/spree/admin/affiliate_settings/show.html.haml @@ -0,0 +1,27 @@ += render 'spree/admin/shared/configuration_menu' + +%h1= t('affiliate_settings') + +%table + %tr + %th{ scope: 'row' } + = Spree.t('sender_credit_on_register_amount') + \: + %td= number_to_currency SpreeAffiliate::Config[:sender_credit_on_register_amount] + %tr + %th{ scope: 'row' } + = Spree.t('recipient_credit_on_register_amount') + \: + %td= number_to_currency SpreeAffiliate::Config[:recipient_credit_on_register_amount] + %tr + %th{ scope: 'row' } + = Spree.t('sender_credit_on_purchase_amount') + \: + %td= number_to_currency SpreeAffiliate::Config[:sender_credit_on_purchase_amount] + %tr + %th{ scope: 'row' } + = Spree.t('recipient_credit_on_purchase_amount') + \: + %td= number_to_currency SpreeAffiliate::Config[:recipient_credit_on_purchase_amount] + +%p= link_to_with_icon 'edit', Spree.t('actions.edit'), edit_admin_affiliate_settings_path diff --git a/app/views/spree/affiliates/_affiliate.html.haml b/app/views/spree/affiliates/_affiliate.html.haml new file mode 100644 index 0000000..a5aab52 --- /dev/null +++ b/app/views/spree/affiliates/_affiliate.html.haml @@ -0,0 +1,4 @@ +%tr{ class: cycle('even', 'odd') } + %td= affiliate.user.email + %td= l affiliate.created_at.to_date + diff --git a/app/views/spree/affiliates/_affiliates.html.haml b/app/views/spree/affiliates/_affiliates.html.haml new file mode 100644 index 0000000..ce5e41d --- /dev/null +++ b/app/views/spree/affiliates/_affiliates.html.haml @@ -0,0 +1,17 @@ +.account-my-affiliates{ data: { hook: 'affiliates' } } + %h3= t(:affiliates_signed_up) + + %strong + = t('affiliate_link') + = link_to_referral_url(@user) + + - if @affiliates.present? + %table + %thead#my-affiliates + %tr + %th= t(:affiliate_email) + %th= t(:affiliation_date) + %tbody= render @affiliates + - else + %p= t(:no_affiliates_yet) + diff --git a/app/views/users/_affiliate.html.erb b/app/views/users/_affiliate.html.erb deleted file mode 100644 index 55000a1..0000000 --- a/app/views/users/_affiliate.html.erb +++ /dev/null @@ -1,15 +0,0 @@ -

- <%= t('affiliate_link') %>: - <%= link_to root_url(:ref_id => @user.ref_id), root_url(:ref_id => @user.ref_id) %>
- <%= link_to t('send_affiliate_link'), email_to_friend_url('affiliate', current_user.find_or_create_affiliate) - %> -

-<% @affiliates = @user.affiliates.where("affiliate_email IS NOT NULL") %> -<% if @affiliates.count > 0 %> -

<%= t('affiliate_links_already_sent_to') %>

- -<% end %> diff --git a/bin/rails b/bin/rails new file mode 100644 index 0000000..890ff31 --- /dev/null +++ b/bin/rails @@ -0,0 +1,7 @@ +# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application. + +ENGINE_ROOT = File.expand_path('../..', __FILE__) +ENGINE_PATH = File.expand_path('../../lib/spree_affiliate/engine', __FILE__) + +require 'rails/all' +require 'rails/engine/commands' diff --git a/config/database.yml b/config/database.yml deleted file mode 100644 index 4836b04..0000000 --- a/config/database.yml +++ /dev/null @@ -1,17 +0,0 @@ -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 diff --git a/config/environments/cucumber.rb b/config/environments/cucumber.rb deleted file mode 100644 index 132dc8d..0000000 --- a/config/environments/cucumber.rb +++ /dev/null @@ -1,38 +0,0 @@ -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 diff --git a/config/initializers/load_configuration.rb b/config/initializers/load_configuration.rb new file mode 100644 index 0000000..e844d13 --- /dev/null +++ b/config/initializers/load_configuration.rb @@ -0,0 +1,3 @@ +module SpreeAffiliate + Config = SpreeAffiliate::Configuration.new +end diff --git a/config/locales/en.yml b/config/locales/en.yml index aea6260..7da354a 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1,12 +1,14 @@ en: - affiliate_link: "Affiliate link" - affiliate_links_already_sent_to: "Already sent to:" - affiliate_program: "Affiliate Program" - affiliate_settings: "Affiliate settings" - manage_affiliate_settings: "Manage affiliate settings" - recipient_credit_on_purchase_amount: "Recipient credit amount (on first order)" - recipient_credit_on_register_amount: "Recipient credit amount (on register)" - registered: "registered" - send_affiliate_link: "Send affiliate link to friend" - sender_credit_on_purchase_amount: "Sender credit amount (on recipient's first order)" - sender_credit_on_register_amount: "Sender credit amount (on recipient register)" + affiliate_link: 'Invite link:' + affiliate_program: Affiliate Program + affiliate_settings: Affiliate settings + affiliates_signed_up: 'Friends signed up:' + edit: Edit + manage_affiliate_settings: Manage affiliate settings + no_affiliates_yet: None of your friends have accepted your invitation + recipient_credit_on_purchase_amount: Recipient credit amount (on first order) + recipient_credit_on_register_amount: Recipient credit amount (on register) + registered: registered + send_affiliate_link: Send affiliate link to friend + sender_credit_on_purchase_amount: Sender credit amount (on recipient's first order) + sender_credit_on_register_amount: Sender credit amount (on recipient register) diff --git a/config/routes.rb b/config/routes.rb index 1f2b42b..841dfef 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,7 @@ -Rails.application.routes.draw do +Spree::Core::Engine.routes.append do namespace :admin do resource :affiliate_settings end - + resources :affiliates, :only => [:show, :index] end diff --git a/db/migrate/20101022121217_create_affiliates.rb b/db/migrate/20101022121217_create_affiliates.rb index 7116ece..d9247ae 100644 --- a/db/migrate/20101022121217_create_affiliates.rb +++ b/db/migrate/20101022121217_create_affiliates.rb @@ -1,14 +1,13 @@ class CreateAffiliates < ActiveRecord::Migration def self.up - create_table :affiliates do |t| + create_table :spree_affiliates do |t| t.integer :user_id t.integer :partner_id - t.string :affiliate_email t.timestamps end end def self.down - drop_table :affiliates + drop_table :spree_affiliates end end diff --git a/db/migrate/20101104145139_create_affiliate_events.rb b/db/migrate/20101104145139_create_affiliate_events.rb index 4759051..61caf0c 100644 --- a/db/migrate/20101104145139_create_affiliate_events.rb +++ b/db/migrate/20101104145139_create_affiliate_events.rb @@ -1,6 +1,6 @@ class CreateAffiliateEvents < ActiveRecord::Migration def self.up - create_table :affiliate_events do |t| + create_table :spree_affiliate_events do |t| t.string :name t.integer :reward_id t.string :reward_type @@ -11,6 +11,6 @@ def self.up end def self.down - drop_table :affiliate_events + drop_table :spree_affiliate_events end end diff --git a/lib/affiliate_credits.rb b/lib/affiliate_credits.rb index 7759178..e9da992 100644 --- a/lib/affiliate_credits.rb +++ b/lib/affiliate_credits.rb @@ -1,29 +1,49 @@ module AffiliateCredits + private - def create_affiliate_credits(sender, recipient, event) - #check if sender should receive credit on affiliate register - if sender_credit_amount = Spree::Config["sender_credit_on_#{event}_amount".to_sym] and sender_credit_amount.to_f > 0 - credit = StoreCredit.create(:amount => sender_credit_amount, - :remaining_amount => sender_credit_amount, - :reason => "Affiliate: #{event}", :user => sender) + def create_affiliate_credits(sender, recipient, event, extra = {}) + #check if sender should receive credit on affiliate register + if sender_credit_amount = SpreeAffiliate::Config["sender_credit_on_#{event}_amount".to_sym] and sender_credit_amount.to_f > 0 + credit = Spree::StoreCredit.create!({:amount => sender_credit_amount, + :remaining_amount => sender_credit_amount, + :reason => "Affiliate: #{event}", + :user => sender}) + log_event recipient.affiliate_partner, sender, credit, event + + ActiveSupport::Notifications.instrument('spree.affiliate.create_credits.sender', {credit: credit, recipient: recipient , event: event, sender: sender }.merge(extra)) + end - log_event recipient.affiliate_partner, sender, credit, event - end - #check if affiliate should recevied credit on sign up - if recipient_credit_amount = Spree::Config["recipient_credit_on_#{event}_amount".to_sym] and recipient_credit_amount.to_f > 0 - credit = StoreCredit.create(:amount => recipient_credit_amount, - :remaining_amount => recipient_credit_amount, - :reason => "Affiliate: #{event}", :user => recipient) + #check if affiliate should recevied credit on sign up + if recipient_credit_amount = SpreeAffiliate::Config["recipient_credit_on_#{event}_amount".to_sym] and recipient_credit_amount.to_f > 0 + credit = Spree::StoreCredit.create!({:amount => recipient_credit_amount, + :remaining_amount => recipient_credit_amount, + :reason => "Affiliate: #{event}", + :user => recipient }) + log_event recipient.affiliate_partner, recipient, credit, event + ActiveSupport::Notifications.instrument('spree.affiliate.create_credits.sender', {credit: credit, recipient: recipient , event: event, sender: sender}.merge(extra)) + # fire_event('spree.affiliate.create_credits.recipient', credit: credit, recipient: recipient, event: event ) + end - log_event recipient.affiliate_partner, recipient, credit, event end - end + def log_event(affiliate, user, credit, event) + affiliate.events.create({:reward => credit, :name => event, :user => user}) + end + + def check_affiliate + @user.reload if @user.present? and not @user.new_record? + return if cookies[:ref_id].blank? || @user.nil? || @user.invalid? + sender = Spree.user_class.find_by_ref_id(cookies[:ref_id]) - def log_event(affiliate, user, credit, event) - affiliate.events.create(:reward => credit, :name => event, :user => user) - end + if sender + sender.affiliates.create(:user_id => @user.id) + #create credit (if required) + @credited = create_affiliate_credits(sender, @user, "register") + end + #destroy the cookie, as the affiliate record has been created. + cookies[:ref_id] = nil + end end diff --git a/lib/generators/spree_affiliate/install/install_generator.rb b/lib/generators/spree_affiliate/install/install_generator.rb new file mode 100644 index 0000000..80a955b --- /dev/null +++ b/lib/generators/spree_affiliate/install/install_generator.rb @@ -0,0 +1,31 @@ +module SpreeAffiliate + module Generators + class InstallGenerator < Rails::Generators::Base + + class_option :auto_run_migrations, :type => :boolean, :default => false + + def add_javascripts + append_file 'vendor/assets/javascripts/spree/frontend/all.js', "//= require spree/frontend/spree_affiliate\n" + append_file 'vendor/assets/javascripts/spree/backend/all.js', "//= require spree/backend/spree_affiliate\n" + end + + def add_stylesheets + inject_into_file 'vendor/assets/stylesheets/spree/frontend/all.css', " *= require spree/frontend/spree_affiliate\n", :before => /\*\//, :verbose => true + inject_into_file 'vendor/assets/stylesheets/spree/backend/all.css', " *= require spree/backend/spree_affiliate\n", :before => /\*\//, :verbose => true + end + + def add_migrations + run 'bundle exec rake railties:install:migrations FROM=spree_affiliate' + end + + def run_migrations + run_migrations = options[:auto_run_migrations] || ['', 'y', 'Y'].include?(ask 'Would you like to run the migrations now? [Y/n]') + if run_migrations + run 'bundle exec rake db:migrate' + else + puts 'Skipping rake db:migrate, don\'t forget to run it!' + end + end + end + end +end diff --git a/lib/spree_affiliate.rb b/lib/spree_affiliate.rb index b9aab37..39d5ec9 100644 --- a/lib/spree_affiliate.rb +++ b/lib/spree_affiliate.rb @@ -1,18 +1,2 @@ require 'spree_core' -require 'spree_affiliate_hooks' - -module SpreeAffiliate - 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 +require 'spree_affiliate/engine' \ No newline at end of file diff --git a/lib/spree_affiliate/configuration.rb b/lib/spree_affiliate/configuration.rb new file mode 100644 index 0000000..38225e1 --- /dev/null +++ b/lib/spree_affiliate/configuration.rb @@ -0,0 +1,8 @@ +module SpreeAffiliate + class Configuration < Spree::Preferences::Configuration + preference :sender_credit_on_purchase_amount, :decimal, :default => 0.0 + preference :sender_credit_on_register_amount, :decimal, :default => 0.0 + preference :recipient_credit_on_register_amount, :decimal, :default => 0.0 + preference :recipient_credit_on_purchase_amount, :decimal, :default => 0.0 + end +end diff --git a/lib/spree_affiliate/engine.rb b/lib/spree_affiliate/engine.rb new file mode 100644 index 0000000..f134e3c --- /dev/null +++ b/lib/spree_affiliate/engine.rb @@ -0,0 +1,22 @@ +module SpreeAffiliate + class Engine < Rails::Engine + require 'spree/core' + isolate_namespace Spree + engine_name 'spree_affiliate' + + config.autoload_paths += %W(#{config.root}/lib) + + # use rspec for tests + config.generators do |g| + g.test_framework :rspec + end + + def self.activate + Dir.glob(File.join(File.dirname(__FILE__), '../../app/**/*_decorator*.rb')) do |c| + Rails.configuration.cache_classes ? require(c) : load(c) + end + end + + config.to_prepare &method(:activate).to_proc + end +end diff --git a/lib/spree_affiliate/factories.rb b/lib/spree_affiliate/factories.rb new file mode 100644 index 0000000..a855b36 --- /dev/null +++ b/lib/spree_affiliate/factories.rb @@ -0,0 +1,6 @@ +FactoryGirl.define do + # Define your Spree extensions Factories within this file to enable applications, and other extensions to use and override them. + # + # Example adding this to your spec_helper will load these Factories for use: + # require 'spree_affiliate/factories' +end diff --git a/lib/spree_affiliate_hooks.rb b/lib/spree_affiliate_hooks.rb deleted file mode 100644 index 1e6b80e..0000000 --- a/lib/spree_affiliate_hooks.rb +++ /dev/null @@ -1,9 +0,0 @@ -class SpreeAffiliateHooks < Spree::ThemeSupport::HookListener - insert_after :admin_configurations_menu do - "<%= configurations_menu_item(I18n.t('affiliate_settings'), admin_affiliate_settings_path, I18n.t('manage_affiliate_settings')) %>" - end - - insert_before :account_my_orders, :partial => 'users/affiliate' - - replace :send_mail_fields, :partial => 'email_sender/fields' -end diff --git a/lib/tasks/install.rake b/lib/tasks/install.rake deleted file mode 100644 index 7291b13..0000000 --- a/lib/tasks/install.rake +++ /dev/null @@ -1,16 +0,0 @@ -namespace :spree_affiliate do - desc "Copies all migrations and assets (NOTE: This will be obsolete with Rails 3.1)" - task :install do - Rake::Task['spree_affiliate:install:migrations'].invoke - end - - namespace :install do - desc "Copies all migrations (NOTE: This will be obsolete with Rails 3.1)" - task :migrations do - source = File.join(File.dirname(__FILE__), '..', '..', 'db') - destination = File.join(Rails.root, 'db') - Spree::FileUtilz.mirror_files(source, destination) - end - end - -end diff --git a/lib/tasks/spree_affiliate.rake b/lib/tasks/spree_affiliate.rake deleted file mode 100644 index 2c5c4e2..0000000 --- a/lib/tasks/spree_affiliate.rake +++ /dev/null @@ -1 +0,0 @@ -# add custom rake tasks here \ No newline at end of file diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb deleted file mode 100644 index ea00b84..0000000 --- a/spec/models/user_spec.rb +++ /dev/null @@ -1,8 +0,0 @@ -require 'spec_helper' - -describe User do - let(:user) { User.new(:email => "foo@bar.com", :password => "secret", :password_confirmation => "secret") } - it "should have ref id" do - user.ref_id.should_not == nil - end -end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1d28893..38f4e36 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,14 +1,47 @@ -# This file is copied to ~/spec when you run 'ruby script/generate rspec' -# from the project root directory. -ENV["RAILS_ENV"] ||= 'test' -require File.expand_path("../test_app/config/environment", __FILE__) +# Run Coverage report +require 'simplecov' +SimpleCov.start do + add_group 'Controllers', 'app/controllers' + add_group 'Helpers', 'app/helpers' + add_group 'Mailers', 'app/mailers' + add_group 'Models', 'app/models' + add_group 'Views', 'app/views' + add_group 'Libraries', 'lib' +end + +# Configure Rails Environment +ENV['RAILS_ENV'] = 'test' + +require File.expand_path('../dummy/config/environment.rb', __FILE__) + require 'rspec/rails' +require 'database_cleaner' +require 'ffaker' -# Requires supporting files with custom matchers and macros, etc, -# in ./support/ and its subdirectories. -Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f} +# Requires supporting ruby files with custom matchers and macros, etc, +# in spec/support/ and its subdirectories. +Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |f| require f } + +# Requires factories defined in spree_core +require 'spree/testing_support/factories' +require 'spree/testing_support/controller_requests' +require 'spree/testing_support/authorization_helpers' +require 'spree/testing_support/url_helpers' + +# Requires factories defined in lib/spree_affiliate/factories.rb +require 'spree_affiliate/factories' RSpec.configure do |config| + config.include FactoryGirl::Syntax::Methods + + # == URL Helpers + # + # Allows access to Spree's routes in specs: + # + # visit spree.admin_path + # current_path.should eql(spree.products_path) + config.include Spree::TestingSupport::UrlHelpers + # == Mock Framework # # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: @@ -17,11 +50,32 @@ # config.mock_with :flexmock # config.mock_with :rr config.mock_with :rspec + config.color = true + # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures config.fixture_path = "#{::Rails.root}/spec/fixtures" - # If you're not using ActiveRecord, or you'd prefer not to run each of your - # examples within a transaction, comment the following line or assign false - # instead of true. - config.use_transactional_fixtures = true + # Capybara javascript drivers require transactional fixtures set to false, and we use DatabaseCleaner + # to cleanup after each test instead. Without transactional fixtures set to false the records created + # to setup a test will be unavailable to the browser, which runs under a seperate server instance. + config.use_transactional_fixtures = false + + # Ensure Suite is set to use transactions for speed. + config.before :suite do + DatabaseCleaner.strategy = :transaction + DatabaseCleaner.clean_with :truncation + end + + # Before each spec check if it is a Javascript test and switch between using database transactions or not where necessary. + config.before :each do + DatabaseCleaner.strategy = example.metadata[:js] ? :truncation : :transaction + DatabaseCleaner.start + end + + # After each spec clean the database. + config.after :each do + DatabaseCleaner.clean + end + + config.fail_fast = ENV['FAIL_FAST'] || false end diff --git a/spree_affiliate.gemspec b/spree_affiliate.gemspec index a99e591..8202dd8 100644 --- a/spree_affiliate.gemspec +++ b/spree_affiliate.gemspec @@ -1,24 +1,34 @@ -# -*- encoding: utf-8 -*- - +# encoding: UTF-8 Gem::Specification.new do |s| s.platform = Gem::Platform::RUBY - s.name = 'spree_affiliate' - s.version = '1.0.0' + s.version = '2.2.1' + s.authors = ['Rails Dog'] s.email = 'gems@railsdog.com' s.homepage = 'http://github.com/spree/spree_affiliate' s.summary = 'Affiliate support for Spree' s.description = 'Affiliate support for Spree' - s.required_ruby_version = '>= 1.8.7' - s.rubygems_version = '1.3.6' - s.files = `git ls-files`.split("\n") - s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") - s.require_paths = ["lib"] - s.add_dependency('spree_core', '>= 0.40.0') - s.add_dependency('spree_auth', '>= 0.40.0') - s.add_dependency('spree_store_credits', '>= 1.0.0') - s.add_dependency('spree_email_to_friend', '>= 1.0.0') + s.required_ruby_version = '>= 1.9.3' + + + s.files = `git ls-files`.split("\n") + s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") + s.require_path = 'lib' + s.requirements << 'none' + + s.add_dependency 'spree_core', '~> 2.2.1' + + s.add_development_dependency 'capybara', '~> 2.1' + s.add_development_dependency 'coffee-rails' + s.add_development_dependency 'database_cleaner' + s.add_development_dependency 'factory_girl', '~> 4.4' + s.add_development_dependency 'ffaker' + s.add_development_dependency 'rspec-rails', '~> 2.13' + s.add_development_dependency 'sass-rails' + s.add_development_dependency 'selenium-webdriver' + s.add_development_dependency 'simplecov' + s.add_development_dependency 'sqlite3' end From ebc7dfba7cf91b252523c5d49ad8f48900bd23fa Mon Sep 17 00:00:00 2001 From: Gustavo Robles Date: Wed, 14 May 2014 13:37:47 -0500 Subject: [PATCH 2/9] use try to avoid NoMethodError for nil class --- app/models/spree/user_decorator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb index ebb880f..4f1c07b 100644 --- a/app/models/spree/user_decorator.rb +++ b/app/models/spree/user_decorator.rb @@ -11,7 +11,7 @@ foreign_key: 'user_id' def referred_by - affiliate_partner.partner + affiliate_partner.try :partner end def ref_id From 0ef4d7d9431c6b908d9168e9e21b3501b1a6800e Mon Sep 17 00:00:00 2001 From: Gustavo Robles Date: Wed, 14 May 2014 13:41:16 -0500 Subject: [PATCH 3/9] fix referred users association by adding a source --- app/models/spree/user_decorator.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb index ebb880f..415242b 100644 --- a/app/models/spree/user_decorator.rb +++ b/app/models/spree/user_decorator.rb @@ -5,7 +5,8 @@ foreign_key: 'partner_id' has_many :referred_users, class_name: 'Spree::User', - through: :affiliates + through: :affiliates, + source: :user has_one :affiliate_partner, class_name:'Spree::Affiliate', foreign_key: 'user_id' From 5d0b17c9f553e5c2a88c452565ae2745d6de00cc Mon Sep 17 00:00:00 2001 From: Gustavo Robles Date: Thu, 15 May 2014 10:37:55 -0500 Subject: [PATCH 4/9] use token instead of id --- app/controllers/spree/base_controller_decorator.rb | 2 +- app/helpers/spree/base_helper_decorator.rb | 2 +- app/models/spree/affiliate.rb | 4 ++++ app/models/spree/user_decorator.rb | 4 ++++ lib/affiliate_credits.rb | 7 ++++--- 5 files changed, 14 insertions(+), 5 deletions(-) diff --git a/app/controllers/spree/base_controller_decorator.rb b/app/controllers/spree/base_controller_decorator.rb index 0b7d777..310b710 100644 --- a/app/controllers/spree/base_controller_decorator.rb +++ b/app/controllers/spree/base_controller_decorator.rb @@ -4,6 +4,6 @@ private def remember_affiliate - cookies.permanent[:ref_id] = params[:ref_id] if params[:ref_id] + cookies.permament[:ref_token] = params[:ref_token] if params[:ref_token] end end diff --git a/app/helpers/spree/base_helper_decorator.rb b/app/helpers/spree/base_helper_decorator.rb index 84130ec..bccc7c2 100644 --- a/app/helpers/spree/base_helper_decorator.rb +++ b/app/helpers/spree/base_helper_decorator.rb @@ -1,6 +1,6 @@ Spree::BaseHelper.class_eval do def referral_url(referrer) - root_url(ref_id: referrer.ref_id) + root_url(ref_token: referrer.ref_token) end def link_to_referral_url(referrer, options = {}) diff --git a/app/models/spree/affiliate.rb b/app/models/spree/affiliate.rb index 35837f2..bac3e4a 100644 --- a/app/models/spree/affiliate.rb +++ b/app/models/spree/affiliate.rb @@ -14,4 +14,8 @@ def name def ref_id partner.try(:ref_id) || '' end + + def state + user_id.nil? ? 'pending' : 'accepted' + end end diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb index 6580bef..bc6a96d 100644 --- a/app/models/spree/user_decorator.rb +++ b/app/models/spree/user_decorator.rb @@ -19,6 +19,10 @@ def ref_id self.id.to_s.reverse end + def ref_token + Base64.encode64 email + end + def self.find_by_ref_id(ref_id) Spree::User.find(ref_id.to_s.reverse) end diff --git a/lib/affiliate_credits.rb b/lib/affiliate_credits.rb index e9da992..68dba5b 100644 --- a/lib/affiliate_credits.rb +++ b/lib/affiliate_credits.rb @@ -34,8 +34,9 @@ def log_event(affiliate, user, credit, event) def check_affiliate @user.reload if @user.present? and not @user.new_record? - return if cookies[:ref_id].blank? || @user.nil? || @user.invalid? - sender = Spree.user_class.find_by_ref_id(cookies[:ref_id]) + return if cookies[:ref_token].blank? || @user.nil? || @user.invalid? + email = Base64.decode cookies[:ref_token] + sender = Spree.user_class.find_by(email: email) if sender sender.affiliates.create(:user_id => @user.id) @@ -44,6 +45,6 @@ def check_affiliate end #destroy the cookie, as the affiliate record has been created. - cookies[:ref_id] = nil + cookies[:ref_token] = nil end end From 24bd868e11991f61cf3ec8ff8ce8a51220408c9c Mon Sep 17 00:00:00 2001 From: Gustavo Robles Date: Thu, 15 May 2014 10:58:04 -0500 Subject: [PATCH 5/9] fix typos --- app/controllers/spree/base_controller_decorator.rb | 2 +- lib/affiliate_credits.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/spree/base_controller_decorator.rb b/app/controllers/spree/base_controller_decorator.rb index 310b710..7051d89 100644 --- a/app/controllers/spree/base_controller_decorator.rb +++ b/app/controllers/spree/base_controller_decorator.rb @@ -4,6 +4,6 @@ private def remember_affiliate - cookies.permament[:ref_token] = params[:ref_token] if params[:ref_token] + cookies.permanent[:ref_token] = params[:ref_token] if params[:ref_token] end end diff --git a/lib/affiliate_credits.rb b/lib/affiliate_credits.rb index 68dba5b..38ec14a 100644 --- a/lib/affiliate_credits.rb +++ b/lib/affiliate_credits.rb @@ -35,7 +35,7 @@ def log_event(affiliate, user, credit, event) def check_affiliate @user.reload if @user.present? and not @user.new_record? return if cookies[:ref_token].blank? || @user.nil? || @user.invalid? - email = Base64.decode cookies[:ref_token] + email = Base64.decode64 cookies[:ref_token] sender = Spree.user_class.find_by(email: email) if sender From d91cd033c6e7a4a4ee27926540e6214315f63520 Mon Sep 17 00:00:00 2001 From: Gustavo Robles Date: Thu, 15 May 2014 10:59:19 -0500 Subject: [PATCH 6/9] remove state method, seems useless --- app/models/spree/affiliate.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/models/spree/affiliate.rb b/app/models/spree/affiliate.rb index bac3e4a..71386d4 100644 --- a/app/models/spree/affiliate.rb +++ b/app/models/spree/affiliate.rb @@ -15,7 +15,4 @@ def ref_id partner.try(:ref_id) || '' end - def state - user_id.nil? ? 'pending' : 'accepted' - end end From 36cf525728b39ef36eae708dc512f2f13761a356 Mon Sep 17 00:00:00 2001 From: Gustavo Robles Date: Wed, 21 May 2014 10:54:54 -0500 Subject: [PATCH 7/9] add haml dependency to avoid errors --- spree_affiliate.gemspec | 1 + 1 file changed, 1 insertion(+) diff --git a/spree_affiliate.gemspec b/spree_affiliate.gemspec index 8202dd8..cc2c0bd 100644 --- a/spree_affiliate.gemspec +++ b/spree_affiliate.gemspec @@ -20,6 +20,7 @@ Gem::Specification.new do |s| s.requirements << 'none' s.add_dependency 'spree_core', '~> 2.2.1' + s.add_dependency 'haml' s.add_development_dependency 'capybara', '~> 2.1' s.add_development_dependency 'coffee-rails' From a381b69c7e5462e1c82c5e0e157bdad5be11f4ff Mon Sep 17 00:00:00 2001 From: Gustavo Robles Date: Wed, 11 Jun 2014 11:53:00 -0500 Subject: [PATCH 8/9] explicitly require haml in engine definition, wasn't being properly loaded --- lib/spree_affiliate/engine.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/spree_affiliate/engine.rb b/lib/spree_affiliate/engine.rb index f134e3c..b4aff69 100644 --- a/lib/spree_affiliate/engine.rb +++ b/lib/spree_affiliate/engine.rb @@ -1,6 +1,7 @@ module SpreeAffiliate class Engine < Rails::Engine require 'spree/core' + require 'haml' isolate_namespace Spree engine_name 'spree_affiliate' From ba8790cd94520b05de29e9c4df2d7059e362848e Mon Sep 17 00:00:00 2001 From: Gustavo Robles Date: Tue, 22 Jul 2014 12:18:12 -0500 Subject: [PATCH 9/9] load right haml dependency --- Gemfile | 8 ++------ spree_affiliate.gemspec | 2 +- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/Gemfile b/Gemfile index efbf03e..1f0ea7c 100644 --- a/Gemfile +++ b/Gemfile @@ -2,12 +2,8 @@ source 'https://rubygems.org' gem 'haml-rails' -gem 'spree_auth_devise', - github: 'spree/spree_auth_devise', - branch: '2-2-stable' +gem 'spree_auth_devise', github: 'spree/spree_auth_devise', branch: '2-2-stable' -gem 'spree_store_credits', - github: 'spree/spree_store_credits', - branch: '2-2-stable' +gem 'spree_store_credits', github: 'spree/spree_store_credits', branch: '2-2-stable' gemspec diff --git a/spree_affiliate.gemspec b/spree_affiliate.gemspec index cc2c0bd..f87aac6 100644 --- a/spree_affiliate.gemspec +++ b/spree_affiliate.gemspec @@ -20,7 +20,7 @@ Gem::Specification.new do |s| s.requirements << 'none' s.add_dependency 'spree_core', '~> 2.2.1' - s.add_dependency 'haml' + s.add_dependency 'haml-rails' s.add_development_dependency 'capybara', '~> 2.1' s.add_development_dependency 'coffee-rails'