spree_email_to_friend 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ *.swp
2
+ spec/test_app
@@ -0,0 +1,3 @@
1
+ = Email To Friend
2
+
3
+ Description goes here
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+
5
+ desc "Default Task"
6
+ task :default => [ :spec ]
7
+
8
+ require 'rspec/core/rake_task'
9
+ RSpec::Core::RakeTask.new
10
+
11
+ require 'cucumber/rake/task'
12
+ Cucumber::Rake::Task.new do |t|
13
+ t.cucumber_opts = %w{--format pretty}
14
+ end
15
+
16
+ desc "Regenerates a rails 3 app for testing"
17
+ task :test_app do
18
+ SPREE_PATH = ENV['SPREE_PATH']
19
+ raise "SPREE_PATH should be specified" unless SPREE_PATH
20
+ require File.join(SPREE_PATH, 'lib/generators/spree/test_app_generator')
21
+ class AuthTestAppGenerator < Spree::Generators::TestAppGenerator
22
+ def tweak_gemfile
23
+ append_file 'Gemfile' do
24
+ <<-gems
25
+ gem 'spree_core', :path => '#{File.join(SPREE_PATH, 'core')}'
26
+ gem 'spree_auth', :path => '#{File.join(SPREE_PATH, 'auth')}'
27
+ gem 'spree_email_to_friend', :path => '#{File.dirname(__FILE__)}'
28
+ gems
29
+ end
30
+ end
31
+
32
+ def install_gems
33
+ system("cd spec/test_app && rake spree_core:install")
34
+ system("cd spec/test_app && rake spree_auth:install")
35
+ end
36
+
37
+ def migrate_db
38
+ run_migrations
39
+ end
40
+ end
41
+ AuthTestAppGenerator.start
42
+ end
43
+
44
+ namespace :test_app do
45
+ desc 'Rebuild test and cucumber databases'
46
+ task :rebuild_dbs do
47
+ system("cd spec/test_app && rake db:drop db:migrate RAILS_ENV=test && rake db:drop db:migrate RAILS_ENV=cucumber")
48
+ end
49
+ end
@@ -0,0 +1,11 @@
1
+ class Admin::CaptchaSettingsController < Admin::BaseController
2
+ def update
3
+ Spree::Captcha::Config.set(params[:preferences])
4
+
5
+ respond_to do |format|
6
+ format.html {
7
+ redirect_to admin_captcha_settings_path
8
+ }
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,53 @@
1
+ class EmailSenderController < Spree::BaseController
2
+ before_filter :find_object
3
+
4
+ def send_mail
5
+ if request.get?
6
+ @mail_to_friend = MailToFriend.new(:sender_email => current_user.try(:email))
7
+ else
8
+ mail_to_friend
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def mail_to_friend
15
+ @mail_to_friend = MailToFriend.new(params[:mail_to_friend])
16
+ respond_to do |format|
17
+ format.html do
18
+ captcha_passed = !Spree::Captcha::Config[:use_captcha] || verify_recaptcha(:private_key => Spree::Captcha::Config[:private_key])
19
+ if @mail_to_friend.valid? && captcha_passed
20
+ flash[:notice] = I18n.t('email_to_friend.mail_sent_to', :email => @mail_to_friend.recipient_email).html_safe
21
+ flash[:notice] << ActionController::Base.helpers.link_to(I18n.t('email_to_friend.send_to_other'), email_to_friend_path(@object.class.name.downcase, @object)).html_safe
22
+
23
+ send_message(@object, @mail_to_friend)
24
+
25
+ method_name = "after_delivering_#{@object.class.name.downcase}_mail"
26
+ send(method_name) if respond_to?(method_name, true)
27
+
28
+ redirect_to @object
29
+ else
30
+ render :action => :send_mail
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ #extract send message to make easier to override
37
+ def send_message(object, mail_to_friend)
38
+ ToFriendMailer.deliver_mail_to_friend(object,@mail_to_friend)
39
+ end
40
+
41
+ def find_object
42
+ class_name = params[:type].titleize.constantize
43
+ return false if params[:id].blank?
44
+ @object = class_name.find_by_id(params[:id])
45
+ if class_name.respond_to?('find_by_permalink')
46
+ @object ||= class_name.find_by_permalink(params[:id])
47
+ end
48
+ if class_name.respond_to?('get_by_param')
49
+ @object ||= class_name.get_by_param(params[:id])
50
+ end
51
+ end
52
+
53
+ end
@@ -0,0 +1,21 @@
1
+ class ToFriendMailer < ActionMailer::Base
2
+ default_url_options[:host] = Spree::Config[:site_url]
3
+ default :from => Spree::Config[:mails_from]
4
+
5
+ def mail_to_friend(object, mail)
6
+ @object = object
7
+ @mail = mail
8
+ opts = {}
9
+
10
+ if mail.hide_recipients && Spree::Config[:hidden_recipients_to_address]
11
+ opts[:to] = Spree::Config[:hidden_recipients_to_address]
12
+ opts[:bcc] = mail.recipient_email
13
+ else
14
+ opts[:to] = mail.recipient_email
15
+ end
16
+ opts[:subject] = mail.subject
17
+ opts[:reply_to] = mail.sender_email
18
+
19
+ mail(opts)
20
+ end
21
+ end
@@ -0,0 +1,9 @@
1
+ class CaptchaConfiguration < Configuration
2
+ THEMES = ['red', 'white', 'blackglass', 'clean', 'custom']
3
+
4
+ # this keys works for localhost
5
+ preference :private_key, :string, :default => '6LfIsgEAAAAAAGfB6Z0lEUtdL3GVuBkRa9cYlMZz'
6
+ preference :public_key, :string, :default => '6LfIsgEAAAAAALpT20eiC3RslZQmmCbiNS-AUvSe'
7
+ preference :theme, :string, :default => 'red'
8
+ preference :use_captcha, :boolean, :default => true
9
+ end
@@ -0,0 +1,46 @@
1
+ class MailToFriend
2
+ include ActiveModel::Validations
3
+ include ActiveModel::Conversion
4
+ attr_accessor :subject, :sender_name, :sender_email, :recipient_name, :recipient_email, :message, :recipients, :invalid_recipients, :hide_recipients
5
+
6
+ EMAILREGEX = /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,8}$/i
7
+
8
+ validates :subject, :presence => true
9
+ validates :sender_name, :presence => true
10
+ validates :recipient_name, :presence => true, :unless => :is_multi
11
+ validates :sender_email, :format => { :with => EMAILREGEX }
12
+ validates :recipients, :length => {:minimum => 1, :message => "must contain at least one valid email address"}
13
+ validates :invalid_recipients, :length => {:maximum => 0, :message => "must be removed"}
14
+
15
+ def initialize(opts = {})
16
+ @subject = opts[:subject] || I18n.t('email_to_friend.you_would_like_this')
17
+ @sender_email = opts[:sender_email] || ' '
18
+ @sender_name = opts[:sender_name] || @sender_email.split('@', 2)[0].titleize
19
+
20
+ @recipients = []
21
+ @invalid_recipients = []
22
+
23
+ @recipient_email = (opts[:recipient_email] || '').gsub(';', ',').gsub(/\s/ , '')
24
+ @recipient_email.split(',').each do |address|
25
+ if address =~ EMAILREGEX
26
+ @recipients << address
27
+ else
28
+ @invalid_recipients << address
29
+ end
30
+ end
31
+
32
+ @recipient_name = opts[:recipient_name]
33
+ @recipient_name ||= @recipients[0].split('@', 2)[0].titleize unless @recipients.empty?
34
+
35
+ @hide_recipients = opts[:hide_recipients] || false
36
+ @message = opts[:message]
37
+ end
38
+
39
+ def persisted?
40
+ false
41
+ end
42
+
43
+ def is_multi
44
+ (@recipients.size + @invalid_recipients.size) > 1
45
+ end
46
+ end
@@ -0,0 +1,22 @@
1
+ <%= render :partial => 'admin/shared/configuration_menu' %>
2
+
3
+ <h1><%= t("captcha.captcha_settings") %></h1>
4
+
5
+ <% form_tag(admin_captcha_settings_path, :method => :put) do -%>
6
+ <p>
7
+ <label><%= t("captcha.public_key") %>:</label>
8
+ <%= text_field_tag('preferences[public_key]', Spree::Captcha::Config[:public_key], :size => 40) %>
9
+ </p>
10
+ <p>
11
+ <label><%= t('captcha.private_key') %>:</label>
12
+ <%= text_field_tag('preferences[private_key]', Spree::Captcha::Config[:private_key], :size => 40)%>
13
+ </p>
14
+ <p>
15
+ <label><%= t('captcha.theme') %>:</label>
16
+ <%= select_tag('preferences[theme]', options_for_select(CaptchaConfiguration::THEMES, Spree::Captcha::Config[:theme])) %>
17
+ </p>
18
+ <p class="form-buttons">
19
+ <%= button t('update') %>
20
+ <%= t("or") %> <%= link_to t("cancel"), admin_captcha_settings_url %>
21
+ </p>
22
+ <% end -%>
@@ -0,0 +1,22 @@
1
+ <%= render :partial => 'admin/shared/configuration_menu' %>
2
+
3
+ <h1><%= t("captcha.captcha_settings") %></h1>
4
+
5
+ <table>
6
+ <tr>
7
+ <th scope="row"><%= t("captcha.public_key") %>:</th>
8
+ <td><%= Spree::Captcha::Config[:public_key] %></td>
9
+ </tr>
10
+ <tr>
11
+ <th scope="row"><%= t("captcha.private_key") %>:</th>
12
+ <td><%= Spree::Captcha::Config[:private_key] %></td>
13
+ </tr>
14
+ <tr>
15
+ <th scope="row"><%= t("captcha.theme") %>:</th>
16
+ <td><%= Spree::Captcha::Config[:theme] %></td>
17
+ </tr>
18
+ </table>
19
+
20
+ <p>
21
+ <%= link_to_with_icon('edit', t("edit"), edit_admin_captcha_settings_path) %>
22
+ </p>
@@ -0,0 +1,33 @@
1
+ <h1><%= t('email_to_friend.tell_about', :product => @object.name) %></h1>
2
+
3
+ <%= form_for(@mail_to_friend, :url => "") do |f| -%>
4
+ <%= render "shared/error_messages", :target => @mail_to_friend %>
5
+ <%= hook :send_mail_fields, {:f => f} do %>
6
+ <p>
7
+ <%= f.label(:sender_name, t('email_to_friend.sender_name')) %><br/>
8
+ <%= f.text_field(:sender_name) %>
9
+ </p>
10
+ <p>
11
+ <%= f.label(:sender_email, t('email_to_friend.sender_email')) %><br/>
12
+ <%= f.text_field(:sender_email) %>
13
+ </p>
14
+ <p>
15
+ <%= f.label(:recipient_name, t('email_to_friend.recipient_name')) %><br/>
16
+ <%= f.text_field(:recipient_name) %>
17
+ </p>
18
+ <p>
19
+ <%= f.label(:recipient_email, t('email_to_friend.recipient_email')) %><br/>
20
+ <%= f.text_field(:recipient_email) %>
21
+ </p>
22
+ <p>
23
+ <%= f.label(:message, t('email_to_friend.message')) %><br/>
24
+ <%= f.text_area(:message) %>
25
+ </p>
26
+ <% end %>
27
+ <% if Spree::Captcha::Config[:use_captcha] %>
28
+ <%= recaptcha_tags :public_key => Spree::Captcha::Config[:public_key], :display => {:theme => Spree::Captcha::Config[:theme]} %>
29
+ <% end %>
30
+ <p>
31
+ <%= f.submit(t('email_to_friend.send_message')) %>
32
+ </p>
33
+ <% end -%>
@@ -0,0 +1,16 @@
1
+ <%= mail.recipient_name %>,
2
+
3
+ I think you would like this item:
4
+
5
+ * <%= product.name %>
6
+
7
+ <% unless mail.message.blank? -%>
8
+ <%= mail.message %>
9
+ <% end -%>
10
+
11
+ <%= mail.sender_name %>
12
+
13
+ Click the link below to view the <%= product.name %> page.
14
+ <%= product_url(product) %>
15
+
16
+ For more online shopping, visit <%= products_url %>
@@ -0,0 +1,3 @@
1
+ <% object_sym = @object.class.name.downcase %>
2
+ <%= render :partial => "#{object_sym.pluralize}/mail_to_friend",
3
+ :locals => {object_sym.to_sym => @object, :mail => @mail} %>
@@ -0,0 +1,22 @@
1
+ ---
2
+ en:
3
+ email_to_friend:
4
+ send_to_friend: "Email a friend"
5
+ tell_about: "Tell a friend about: %{product}"
6
+ sender_name: "Your name"
7
+ sender_email: "Your email address"
8
+ recipient_name: "Your friend's name"
9
+ recipient_email: "Your friend's email address"
10
+ recipient_emails: "Email addresses (comma separated)"
11
+ message: "Message"
12
+ send_message: "Tell your friend!"
13
+ mail_sent_to: "Mail sent to %{email} "
14
+ send_to_other: "Send to other friend"
15
+ you_would_like_this: "I think you would like this item"
16
+
17
+ captcha:
18
+ captcha_settings: "Captcha Settings"
19
+ public_key: "Public key"
20
+ private_key: "Private key"
21
+ manage_keys: "Manage your (re)captcha keys"
22
+ theme: "Theme"
@@ -0,0 +1,20 @@
1
+ ---
2
+ es:
3
+ email_to_friend:
4
+ send_to_friend: "Enviar a un amigo"
5
+ tell_about: "Enviar {{product}} a un amigo"
6
+ sender_name: "Tu nombre"
7
+ sender_email: "Tu dirección de correo"
8
+ recipient_name: "Nombre de tu amigo"
9
+ recipient_email: "Dirección de correo de tu amigo"
10
+ message: "Mensaje"
11
+ send_message: "¡Enviar a un amigo!"
12
+ mail_sent_to: "Mensaje enviado a {{email}} "
13
+ send_to_other: "Enviar a otro amigo"
14
+ you_would_like_this: "Creo que este artículo te gustará"
15
+ captcha:
16
+ captcha_settings: "Configuracion de Captcha"
17
+ public_key: "Llave pública"
18
+ private_key: "Llave privada"
19
+ manage_keys: "Administrar tus llaves de recaptcha."
20
+ theme: "Tema"
@@ -0,0 +1,9 @@
1
+ Rails.application.routes.draw do
2
+
3
+ match "/email_to_friend/:type/:id" => 'email_sender#send_mail', :as => :email_to_friend
4
+
5
+ namespace :admin do
6
+ resource :captcha_settings
7
+ end
8
+
9
+ end
@@ -0,0 +1,10 @@
1
+ module Spree::Captcha
2
+ class Config < Spree::Config
3
+ class << self
4
+ def instance
5
+ return nil unless ActiveRecord::Base.connection.tables.include?('configurations')
6
+ CaptchaConfiguration.find_or_create_by_name("Captcha configuration")
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,18 @@
1
+ require 'spree_core'
2
+ require 'spree_email_to_friend_hooks'
3
+ require 'recaptcha/rails'
4
+
5
+ module SpreeEmailToFriend
6
+ class Engine < Rails::Engine
7
+
8
+ config.autoload_paths += %W(#{config.root}/lib)
9
+
10
+ def self.activate
11
+ Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c|
12
+ Rails.env == "production" ? require(c) : load(c)
13
+ end
14
+ end
15
+
16
+ config.to_prepare &method(:activate).to_proc
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ class SpreeEmailToFriendHooks < Spree::ThemeSupport::HookListener
2
+ insert_after :admin_configurations_menu do
3
+ "<%= configurations_menu_item(I18n.t('captcha.captcha_settings'), admin_captcha_settings_path, I18n.t('captcha.manage_keys')) %>"
4
+ end
5
+
6
+ insert_after :product_description do
7
+ %(<p class="email_to_friend">
8
+ <%= link_to(t('email_to_friend.send_to_friend'), email_to_friend_url('product', @product)) %>
9
+ </p>)
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe EmailSenderController do
4
+
5
+ #Delete this example and add some real ones
6
+ it "should use EmailSenderController" do
7
+ controller.should be_an_instance_of(EmailSenderController)
8
+ end
9
+
10
+ end
@@ -0,0 +1,6 @@
1
+ --colour
2
+ --format
3
+ progress
4
+ --loadby
5
+ mtime
6
+ --reverse
@@ -0,0 +1,28 @@
1
+ # This file is copied to ~/spec when you run 'ruby script/generate rspec'
2
+ # from the project root directory.
3
+ ENV["RAILS_ENV"] ||= 'test'
4
+ require File.expand_path("../test_app/config/environment", __FILE__)
5
+ require 'rspec/rails'
6
+
7
+ # Requires supporting files with custom matchers and macros, etc,
8
+ # in ./support/ and its subdirectories.
9
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
10
+
11
+ RSpec.configure do |config|
12
+ # == Mock Framework
13
+ #
14
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
15
+ #
16
+ # config.mock_with :mocha
17
+ # config.mock_with :flexmock
18
+ # config.mock_with :rr
19
+ config.mock_with :rspec
20
+
21
+ config.fixture_path = "#{::Rails.root}/spec/fixtures"
22
+
23
+ # If you're not using ActiveRecord, or you'd prefer not to run each of your
24
+ # examples within a transaction, comment the following line or assign false
25
+ # instead of true.
26
+ config.use_transactional_fixtures = true
27
+ end
28
+
@@ -0,0 +1,20 @@
1
+ Gem::Specification.new do |s|
2
+ s.platform = Gem::Platform::RUBY
3
+ s.name = 'spree_email_to_friend'
4
+ s.version = '1.0.0'
5
+ s.summary = 'Spree extension to send product recommendations to friends'
6
+ s.homepage = 'https://github.com/calas/spree-email-to-friend'
7
+ s.required_ruby_version = '>= 1.8.7'
8
+
9
+ s.author = 'Jorge Calás Lozano, Roman Smirnov'
10
+
11
+ s.files = `git ls-files`.split("\n")
12
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
13
+ s.require_paths = ["lib"]
14
+
15
+ s.has_rdoc = true
16
+
17
+ s.add_dependency('spree_core', '>= 0.30.1')
18
+ s.add_dependency('spree_auth', '>= 0.30.1')
19
+ s.add_dependency('recaptcha', '>= 0.3.1')
20
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spree_email_to_friend
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - "Jorge Cal\xC3\xA1s Lozano, Roman Smirnov"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-23 00:00:00 +00:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: spree_core
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 101
30
+ segments:
31
+ - 0
32
+ - 30
33
+ - 1
34
+ version: 0.30.1
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: spree_auth
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 101
46
+ segments:
47
+ - 0
48
+ - 30
49
+ - 1
50
+ version: 0.30.1
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: recaptcha
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 17
62
+ segments:
63
+ - 0
64
+ - 3
65
+ - 1
66
+ version: 0.3.1
67
+ type: :runtime
68
+ version_requirements: *id003
69
+ description:
70
+ email:
71
+ executables: []
72
+
73
+ extensions: []
74
+
75
+ extra_rdoc_files: []
76
+
77
+ files:
78
+ - .gitignore
79
+ - README.markdown
80
+ - Rakefile
81
+ - app/controllers/admin/captcha_settings_controller.rb
82
+ - app/controllers/email_sender_controller.rb
83
+ - app/mailers/to_friend_mailer.rb
84
+ - app/models/captcha_configuration.rb
85
+ - app/models/mail_to_friend.rb
86
+ - app/views/admin/captcha_settings/edit.html.erb
87
+ - app/views/admin/captcha_settings/show.html.erb
88
+ - app/views/email_sender/send_mail.html.erb
89
+ - app/views/products/_mail_to_friend.html.erb
90
+ - app/views/to_friend_mailer/mail_to_friend.erb
91
+ - config/locales/en.yml
92
+ - config/locales/es.yml
93
+ - config/routes.rb
94
+ - lib/spree/captcha/config.rb
95
+ - lib/spree_email_to_friend.rb
96
+ - lib/spree_email_to_friend_hooks.rb
97
+ - spec/controllers/email_sender_controller_spec.rb
98
+ - spec/spec.opts
99
+ - spec/spec_helper.rb
100
+ - spree_email_to_friend.gemspec
101
+ has_rdoc: true
102
+ homepage: https://github.com/calas/spree-email-to-friend
103
+ licenses: []
104
+
105
+ post_install_message:
106
+ rdoc_options: []
107
+
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ hash: 57
116
+ segments:
117
+ - 1
118
+ - 8
119
+ - 7
120
+ version: 1.8.7
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ hash: 3
127
+ segments:
128
+ - 0
129
+ version: "0"
130
+ requirements: []
131
+
132
+ rubyforge_project:
133
+ rubygems_version: 1.3.7
134
+ signing_key:
135
+ specification_version: 3
136
+ summary: Spree extension to send product recommendations to friends
137
+ test_files:
138
+ - spec/controllers/email_sender_controller_spec.rb
139
+ - spec/spec.opts
140
+ - spec/spec_helper.rb