From 997bd52c7f9599d8f2280ecab2e8e9ff958ca00e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Cal=C3=A1s=20Lozano?= Date: Wed, 14 Oct 2009 21:11:11 +0200 Subject: [PATCH] Added captcha configuration stuff --- .../admin/captcha_settings_controller.rb | 11 ++++++ app/models/captcha_configuration.rb | 8 +++++ .../admin/captcha_settings/edit.html.erb | 22 ++++++++++++ .../admin/captcha_settings/show.html.erb | 22 ++++++++++++ app/views/products/tell_a_friend.html.erb | 5 +-- config/locales/en-US.yml | 7 ++++ config/locales/es.yml | 6 ++++ config/routes.rb | 6 ++-- email_to_friend_extension.rb | 35 ++++++------------- lib/spree/captcha/config.rb | 10 ++++++ lib/tell_friend_actions.rb | 2 +- 11 files changed, 101 insertions(+), 33 deletions(-) create mode 100644 app/controllers/admin/captcha_settings_controller.rb create mode 100644 app/models/captcha_configuration.rb create mode 100644 app/views/admin/captcha_settings/edit.html.erb create mode 100644 app/views/admin/captcha_settings/show.html.erb create mode 100644 lib/spree/captcha/config.rb diff --git a/app/controllers/admin/captcha_settings_controller.rb b/app/controllers/admin/captcha_settings_controller.rb new file mode 100644 index 0000000..ee426ee --- /dev/null +++ b/app/controllers/admin/captcha_settings_controller.rb @@ -0,0 +1,11 @@ +class Admin::CaptchaSettingsController < Admin::BaseController + def update + Spree::Captcha::Config.set(params[:preferences]) + + respond_to do |format| + format.html { + redirect_to admin_captcha_settings_path + } + end + end +end diff --git a/app/models/captcha_configuration.rb b/app/models/captcha_configuration.rb new file mode 100644 index 0000000..64f0bb9 --- /dev/null +++ b/app/models/captcha_configuration.rb @@ -0,0 +1,8 @@ +class CaptchaConfiguration < Configuration + THEMES = ['red', 'white', 'blackglass', 'clean', 'custom'] + + # this keys works for localhost + preference :private_key, :string, :default => '6LfIsgEAAAAAAGfB6Z0lEUtdL3GVuBkRa9cYlMZz' + preference :public_key, :string, :default => '6LfIsgEAAAAAALpT20eiC3RslZQmmCbiNS-AUvSe' + preference :theme, :string, :default => 'red' +end diff --git a/app/views/admin/captcha_settings/edit.html.erb b/app/views/admin/captcha_settings/edit.html.erb new file mode 100644 index 0000000..94cc4f6 --- /dev/null +++ b/app/views/admin/captcha_settings/edit.html.erb @@ -0,0 +1,22 @@ +<%= render :partial => 'admin/shared/configuration_menu' %> + +

<%= t("captcha.captcha_settings") %>

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

+ + <%= text_field_tag('preferences[public_key]', Spree::Captcha::Config[:public_key], :size => 40) %> +

+

+ + <%= text_field_tag('preferences[private_key]', Spree::Captcha::Config[:private_key], :size => 40)%> +

+

+ + <%= select_tag('preferences[theme]', options_for_select(CaptchaConfiguration::THEMES, Spree::Captcha::Config[:theme])) %> +

+

+ <%= button t('update') %> + <%= t("or") %> <%= link_to t("cancel"), admin_captcha_settings_url %> +

+<% end -%> diff --git a/app/views/admin/captcha_settings/show.html.erb b/app/views/admin/captcha_settings/show.html.erb new file mode 100644 index 0000000..4b32079 --- /dev/null +++ b/app/views/admin/captcha_settings/show.html.erb @@ -0,0 +1,22 @@ +<%= render :partial => 'admin/shared/configuration_menu' %> + +

<%= t("captcha.captcha_settings") %>

+ + + + + + + + + + + + + + +
<%= t("captcha.public_key") %>:<%= Spree::Captcha::Config[:public_key] %>
<%= t("captcha.private_key") %>:<%= Spree::Captcha::Config[:private_key] %>
<%= t("captcha.theme") %>:<%= Spree::Captcha::Config[:theme] %>
+ +

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

diff --git a/app/views/products/tell_a_friend.html.erb b/app/views/products/tell_a_friend.html.erb index b91acab..6e58bce 100644 --- a/app/views/products/tell_a_friend.html.erb +++ b/app/views/products/tell_a_friend.html.erb @@ -6,7 +6,6 @@ <%= 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) %> @@ -23,9 +22,7 @@ <%= f.label(:message, t('email_to_friend.message')) %>
<%= f.text_area(:message) %>

- - <%= recaptcha_tags :public_key => Spree::Config[:recaptcha_public_key] %> - + <%= recaptcha_tags :public_key => Spree::Captcha::Config[:public_key], :display => {:theme => Spree::Captcha::Config[:theme]} %>

<%= f.submit(t('email_to_friend.send_message')) %>

diff --git a/config/locales/en-US.yml b/config/locales/en-US.yml index 34eb2e9..fc47994 100644 --- a/config/locales/en-US.yml +++ b/config/locales/en-US.yml @@ -12,3 +12,10 @@ en-US: mail_sent_to: "Mail sent to {{email}} " send_to_other: "Send to other friend" you_would_like_this: "I think you would like this item" + + captcha: + captcha_settings: "Captcha Settings" + public_key: "Public key" + private_key: "Private key" + manage_keys: "Manage your (re)captcha keys" + theme: "Theme" \ No newline at end of file diff --git a/config/locales/es.yml b/config/locales/es.yml index de95a88..a47933f 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -12,3 +12,9 @@ es: mail_sent_to: "Mensaje enviado a {{email}} " send_to_other: "Enviar a otro amigo" you_would_like_this: "Creo que este artículo te gustará" + captcha: + captcha_settings: "Configuracion de Captcha" + public_key: "Llave pública" + private_key: "Llave privada" + manage_keys: "Administrar tus llaves de recaptcha." + theme: "Tema" \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 52a0362..17fee9a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,6 +2,6 @@ map.resources :products, :member => { :tell_a_friend => :get, :mail_to_friend => :put } -# map.namespace :admin do |admin| -# admin.resources :whatever -# end +map.namespace :admin do |admin| + admin.resource :captcha_settings +end diff --git a/email_to_friend_extension.rb b/email_to_friend_extension.rb index a80a52e..3011e92 100644 --- a/email_to_friend_extension.rb +++ b/email_to_friend_extension.rb @@ -17,35 +17,20 @@ def self.require_gems(config) end def activate - AppConfiguration.class_eval do - # this keys works for localhost - preference :recaptcha_private_key, :string, :default => '6LfIsgEAAAAAAGfB6Z0lEUtdL3GVuBkRa9cYlMZz' - preference :recaptcha_public_key, :string, :default => '6LfIsgEAAAAAALpT20eiC3RslZQmmCbiNS-AUvSe' - end - ProductsController.class_eval do include TellFriendActions end - # Add your extension tab to the admin. - # Requires that you have defined an admin controller: - # app/controllers/admin/yourextension_controller - # and that you mapped your admin in config/routes + Admin::ConfigurationsController.class_eval do + before_filter :add_captcha_settings_links, :only => :index - #Admin::BaseController.class_eval do - # before_filter :add_yourextension_tab - # - # def add_yourextension_tab - # # add_extension_admin_tab takes an array containing the same arguments expected - # # by the tab helper method: - # # [ :extension_name, { :label => "Your Extension", :route => "/some/non/standard/route" } ] - # add_extension_admin_tab [ :yourextension ] - # end - #end - - # make your helper avaliable in all views - # Spree::BaseController.class_eval do - # helper YourHelper - # end + def add_captcha_settings_links + @extension_links << { + :link => admin_captcha_settings_path, + :link_text => t('captcha.captcha_settings'), + :description => t('captcha.manage_keys') + } + end + end end end diff --git a/lib/spree/captcha/config.rb b/lib/spree/captcha/config.rb new file mode 100644 index 0000000..b74eec6 --- /dev/null +++ b/lib/spree/captcha/config.rb @@ -0,0 +1,10 @@ +module Spree::Captcha + class Config < Spree::Config + class << self + def instance + return nil unless ActiveRecord::Base.connection.tables.include?('configurations') + CaptchaConfiguration.find_or_create_by_name("Captcha configuration") + end + end + end +end diff --git a/lib/tell_friend_actions.rb b/lib/tell_friend_actions.rb index 34b7a21..63b80f8 100644 --- a/lib/tell_friend_actions.rb +++ b/lib/tell_friend_actions.rb @@ -13,7 +13,7 @@ def mail_to_friend respond_to do |format| format.html do if @mail_to_friend.valid? && - verify_recaptcha(:private_key => Spree::Config[:recaptcha_private_key]) + verify_recaptcha(:private_key => Spree::Captcha::Config[:private_key]) flash[:notice] = I18n.t('email_to_friend.mail_sent_to', :email => @mail_to_friend.recipient_email) flash[:notice] << @template.link_to(I18n.t('email_to_friend.send_to_other'), tell_a_friend_product_path(@product))