From 47ee46695c48c164ff572fcbd0404b0cc99cef83 Mon Sep 17 00:00:00 2001 From: xdite Date: Sat, 28 Jun 2014 20:33:24 +0800 Subject: [PATCH 1/5] install setting logic --- .gitignore | 1 + Gemfile | 2 ++ Gemfile.lock | 2 ++ app/models/setting.rb | 4 ++++ config/config.yml.example | 11 +++++++++++ 5 files changed, 20 insertions(+) create mode 100644 app/models/setting.rb create mode 100644 config/config.yml.example diff --git a/.gitignore b/.gitignore index 3d0a5a46..c8fc2c7e 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ /tmp .DS_Store public/uploads +config/config.yml diff --git a/Gemfile b/Gemfile index f2312016..2a1a43ff 100644 --- a/Gemfile +++ b/Gemfile @@ -39,6 +39,8 @@ gem "stripe" gem "aasm" +gem "settingslogic" + group :development do gem "annotate" diff --git a/Gemfile.lock b/Gemfile.lock index 8953a06a..cdcb7363 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -122,6 +122,7 @@ GEM sdoc (0.4.0) json (~> 1.8) rdoc (~> 4.0, < 5.0) + settingslogic (2.0.9) simple_form (3.1.0.rc1) actionpack (~> 4.0) activemodel (~> 4.0) @@ -175,6 +176,7 @@ DEPENDENCIES roadie sass-rails (~> 4.0.3) sdoc (~> 0.4.0) + settingslogic simple_form (= 3.1.0rc1) spring sqlite3 diff --git a/app/models/setting.rb b/app/models/setting.rb new file mode 100644 index 00000000..ab37808e --- /dev/null +++ b/app/models/setting.rb @@ -0,0 +1,4 @@ +class Setting < Settingslogic + source "#{Rails.root}/config/config.yml" + namespace Rails.env +end \ No newline at end of file diff --git a/config/config.yml.example b/config/config.yml.example new file mode 100644 index 00000000..da52c9e0 --- /dev/null +++ b/config/config.yml.example @@ -0,0 +1,11 @@ +defaults: &defaults + app_name: "ArtStore" +development: + <<: *defaults + +test: + <<: *defaults + +production: + <<: *defaults + \ No newline at end of file From aa3649c46b52eb741fc10ea61fd184194510922f Mon Sep 17 00:00:00 2001 From: xdite Date: Sat, 28 Jun 2014 20:36:56 +0800 Subject: [PATCH 2/5] extract stripe key to settings logic --- app/controllers/card_charges_controller.rb | 2 +- app/views/orders/pay_with_credit_card.html.erb | 2 +- config/config.yml.example | 3 +++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/controllers/card_charges_controller.rb b/app/controllers/card_charges_controller.rb index 3c612013..ec727dad 100644 --- a/app/controllers/card_charges_controller.rb +++ b/app/controllers/card_charges_controller.rb @@ -7,7 +7,7 @@ def create @order = current_user.orders.find_by_token(params[:order_id]) @amount = @order.total * 100 # in cents - Stripe.api_key = 'sk_test_iszGpQ7DUU1QsF6oBGnH8bf2' + Stripe.api_key = Setting.stripe.secret_key customer = Stripe::Customer.create( :email => current_user.email, diff --git a/app/views/orders/pay_with_credit_card.html.erb b/app/views/orders/pay_with_credit_card.html.erb index e44b15c8..e21c23eb 100644 --- a/app/views/orders/pay_with_credit_card.html.erb +++ b/app/views/orders/pay_with_credit_card.html.erb @@ -1,7 +1,7 @@ diff --git a/config/config.yml.example b/config/config.yml.example index da52c9e0..0ed82572 100644 --- a/config/config.yml.example +++ b/config/config.yml.example @@ -1,5 +1,8 @@ defaults: &defaults app_name: "ArtStore" + stripe: + secret_key: + publishable_key: development: <<: *defaults From 7f093a7a2b93bc4be5e03bf9423250eb77773a53 Mon Sep 17 00:00:00 2001 From: xdite Date: Sat, 28 Jun 2014 20:42:27 +0800 Subject: [PATCH 3/5] install mailgun replace password with setting --- config/config.yml.example | 3 +++ config/environments/production.rb | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/config/config.yml.example b/config/config.yml.example index 0ed82572..17c8268c 100644 --- a/config/config.yml.example +++ b/config/config.yml.example @@ -3,6 +3,9 @@ defaults: &defaults stripe: secret_key: publishable_key: + mailgun: + username: + password: development: <<: *defaults diff --git a/config/environments/production.rb b/config/environments/production.rb index 47d3553b..dccf9b42 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -80,4 +80,14 @@ # Do not dump schema after migrations. config.active_record.dump_schema_after_migration = false + + config.action_mailer.delivery_method = :smtp + config.action_mailer.smtp_settings = { + address: 'smtp.mailgun.org', + port: 587, + domain: 'sandboxd321bed1624b4744b456caca384107f8.mailgun.org', + user_name: Setting.mailgun.username, + password: Setting.mailgun.password, + authentication: 'plain' + } end From ef0e5167ea9dc5806db9e5be5ef7990f4a6d0270 Mon Sep 17 00:00:00 2001 From: xdite Date: Sat, 28 Jun 2014 20:58:21 +0800 Subject: [PATCH 4/5] extract to stripe charger service --- app/controllers/card_charges_controller.rb | 28 +++++++------------ app/controllers/orders_controller.rb | 3 +-- app/services/stripe_charge.rb | 31 ++++++++++++++++++++++ 3 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 app/services/stripe_charge.rb diff --git a/app/controllers/card_charges_controller.rb b/app/controllers/card_charges_controller.rb index ec727dad..51cd0914 100644 --- a/app/controllers/card_charges_controller.rb +++ b/app/controllers/card_charges_controller.rb @@ -7,29 +7,21 @@ def create @order = current_user.orders.find_by_token(params[:order_id]) @amount = @order.total * 100 # in cents - Stripe.api_key = Setting.stripe.secret_key - - customer = Stripe::Customer.create( - :email => current_user.email, - :card => params[:stripeToken] - ) - - - charge = Stripe::Charge.create( - :customer => customer.id, + charge = StripeCharge.create( :amount => @amount, + :card => params[:stripeToken], :description => @order.token , - :currency => 'usd' ) - @order.set_payment_with!("credit_card") - @order.make_payment! - - redirect_to order_path(@order.token), :notice => "成功完成付款" - - rescue Stripe::CardError => e - flash[:error] = e.message + if charge.successful? + @order.set_payment_with!("credit_card") + @order.make_payment! + redirect_to order_path(@order.token), :notice => "成功完成付款" + else + flash[:error] = charge.error_message render "orders/pay_with_credit_card" + end + end end diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index a332486d..407dcc20 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -7,8 +7,7 @@ def create if @order.save - OrderPlacingService.new(current_cart, order).place_order! - + OrderPlacingService.new(current_cart, @order).place_order! redirect_to order_path(@order.token) else diff --git a/app/services/stripe_charge.rb b/app/services/stripe_charge.rb new file mode 100644 index 00000000..cdf489d9 --- /dev/null +++ b/app/services/stripe_charge.rb @@ -0,0 +1,31 @@ +class StripeCharge + + attr_reader :error_message, :response + + def initialize(options={}) + @response = options[:response] + @error_message = options[:error_message] + end + + def self.create(options={}) + + Stripe.api_key = Setting.stripe.secret_key + + begin + response = Stripe::Charge.create( + amount: options[:amount], + currency: "usd", + card: options[:card], + description: options[:description] + ) + new(:response => response) + rescue Stripe::CardError => e + new(:error_message => e.message) + end + end + + def successful? + @response.present? + end + +end \ No newline at end of file From 9afef36ccace5ce02e386e8a20ef3e99d78d1708 Mon Sep 17 00:00:00 2001 From: xdite Date: Sat, 28 Jun 2014 21:12:12 +0800 Subject: [PATCH 5/5] install ransack & implement search --- Gemfile | 2 ++ Gemfile.lock | 11 +++++++++++ app/controllers/products_controller.rb | 20 ++++++++++++++++++++ app/views/common/_navbar.html.erb | 11 +++++++++++ app/views/products/search.html.erb | 9 +++++++++ config/routes.rb | 3 +++ 6 files changed, 56 insertions(+) create mode 100644 app/views/products/search.html.erb diff --git a/Gemfile b/Gemfile index 2a1a43ff..4ef68647 100644 --- a/Gemfile +++ b/Gemfile @@ -40,6 +40,8 @@ gem "stripe" gem "aasm" gem "settingslogic" +gem "will_paginate" +gem "ransack" group :development do diff --git a/Gemfile.lock b/Gemfile.lock index cdcb7363..430116f9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -84,6 +84,8 @@ GEM nokogiri (1.6.2.1) mini_portile (= 0.6.0) orm_adapter (0.5.0) + polyamorous (1.0.0) + activerecord (>= 3.0) polyglot (0.3.5) rack (1.5.2) rack-test (0.6.2) @@ -104,6 +106,12 @@ GEM rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) rake (10.3.2) + ransack (1.2.3) + actionpack (>= 3.0) + activerecord (>= 3.0) + activesupport (>= 3.0) + i18n + polyamorous (~> 1.0.0) rdoc (4.1.1) json (~> 1.4) rest-client (1.6.7) @@ -157,6 +165,7 @@ GEM json (>= 1.8.0) warden (1.2.3) rack (>= 1.0) + will_paginate (3.0.4) PLATFORMS ruby @@ -173,6 +182,7 @@ DEPENDENCIES letter_opener mini_magick rails (= 4.1.0) + ransack roadie sass-rails (~> 4.0.3) sdoc (~> 0.4.0) @@ -183,3 +193,4 @@ DEPENDENCIES stripe turbolinks uglifier (>= 1.3.0) + will_paginate diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index e2ed10ec..70af446c 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -1,5 +1,7 @@ class ProductsController < ApplicationController + before_filter :validate_search_key, :only => [:search] + def index @products = Product.order("id DESC") end @@ -30,5 +32,23 @@ def add_to_cart end + def search + if @query_string.present? + search_result = Product.ransack(@search_criteria).result(:distinct => true) + @products = search_result.paginate(:page => params[:page], :per_page => 20 ) + end + end + + protected + + def validate_search_key + @query_string = params[:q].gsub(/\\|\'|\/|\?/, "") if params[:q].present? + @search_criteria = search_criteria(@query_string) + end + + + def search_criteria(query_string) + { :title_cont => query_string } + end end diff --git a/app/views/common/_navbar.html.erb b/app/views/common/_navbar.html.erb index 41356da3..1655302c 100644 --- a/app/views/common/_navbar.html.erb +++ b/app/views/common/_navbar.html.erb @@ -14,6 +14,17 @@ + + <%= form_tag search_products_path, :class => "navbar-form navbar-left" do %> +
+ + + +
+ + <% end %> + +