forked from solidusio/solidus_braintree
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request solidusio#44 from solidusio/braintree_configuration
Add Braintree configuration to Spree::Store model
- Loading branch information
Showing
18 changed files
with
244 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module SolidusPaypalBraintree | ||
module RoutesHelper | ||
def method_missing(method_sym, *arguments, &block) | ||
if spree.respond_to?(method_sym) | ||
spree.send(method_sym, arguments) | ||
else | ||
super | ||
end | ||
end | ||
|
||
def respond_to_missing?(method_sym, include_private = false) | ||
spree.respond_to?(method_sym) || super | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class SolidusPaypalBraintree::Configuration < ApplicationRecord | ||
belongs_to :store, class_name: 'Spree::Store' | ||
|
||
validates :store, presence: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Spree::Store.class_eval do | ||
has_one :braintree_configuration, class_name: "SolidusPaypalBraintree::Configuration" | ||
|
||
before_create :build_default_configuration | ||
|
||
private | ||
|
||
def build_default_configuration | ||
build_braintree_configuration | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Deface::Override.new( | ||
virtual_path: "spree/admin/shared/_settings_sub_menu", | ||
name: "solidus_paypal_braintree_admin_navigation_configuration", | ||
insert_bottom: "[data-hook='admin_settings_sub_tabs']", | ||
partial: "solidus_paypal_braintree/configurations/admin_tab" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
en: | ||
spree: | ||
admin: | ||
tab: | ||
braintree: Braintree | ||
solidus_paypal_braintree: | ||
nonce: Nonce | ||
token: Token | ||
payment_type: | ||
apple_pay_card: Apple Pay | ||
pay_pal_account: PayPal | ||
configurations: | ||
title: Braintree Configurations | ||
tab: Braintree | ||
update_success: Successfully updated Braintree configurations. | ||
update_error: An error occurred while updating Braintree configurations. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
Rails.application.routes.draw do | ||
namespace :solidus_paypal_braintree do | ||
resource :checkout, only: [:update, :edit] | ||
resource :client_token, only: [:create], format: :json | ||
resource :transactions, only: [:create] | ||
SolidusPaypalBraintree::Engine.routes.draw do | ||
resource :checkout, only: [:update, :edit] | ||
resource :client_token, only: [:create], format: :json | ||
resource :transactions, only: [:create] | ||
|
||
resources :configurations do | ||
collection do | ||
get :list | ||
post :update | ||
end | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
db/migrate/20161114231422_create_solidus_paypal_braintree_configurations.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class CreateSolidusPaypalBraintreeConfigurations < ActiveRecord::Migration | ||
def change | ||
create_table :solidus_paypal_braintree_configurations do |t| | ||
t.boolean :paypal, null: false, default: false | ||
t.boolean :apple_pay, null: false, default: false | ||
t.integer :store_id, null: false, index: true, foreign_key: { references: :spree_stores } | ||
|
||
t.timestamps null: false | ||
end | ||
end | ||
end |
9 changes: 9 additions & 0 deletions
9
db/migrate/20161125172005_add_braintree_configuration_to_stores.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class AddBraintreeConfigurationToStores < ActiveRecord::Migration | ||
def up | ||
Spree::Store.all.each(&:create_braintree_configuration) | ||
end | ||
|
||
def down | ||
SolidusPaypalBraintree::Configuration.joins(:store).destroy_all | ||
end | ||
end |
30 changes: 30 additions & 0 deletions
30
lib/controllers/backend/solidus_paypal_braintree/configurations_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module SolidusPaypalBraintree | ||
class ConfigurationsController < Spree::Admin::BaseController | ||
helper RoutesHelper | ||
|
||
def list | ||
authorize! :list, SolidusPaypalBraintree::Configuration | ||
|
||
@configurations = Spree::Store.all.map(&:braintree_configuration) | ||
end | ||
|
||
def update | ||
authorize! :update, SolidusPaypalBraintree::Configuration | ||
|
||
params = configurations_params[:configuration_fields] | ||
if SolidusPaypalBraintree::Configuration.update(params.keys, params.values) | ||
flash[:success] = t('update_success', scope: 'solidus_paypal_braintree.configurations') | ||
else | ||
flash[:error] = t('update_error', scope: 'solidus_paypal_braintree.configurations') | ||
end | ||
redirect_to action: :list | ||
end | ||
|
||
private | ||
|
||
def configurations_params | ||
params.require(:configurations). | ||
permit(configuration_fields: [:paypal, :apple_pay]) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
lib/views/backend/solidus_paypal_braintree/configurations/_admin_tab.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<%= tab :braintree, match_path: /braintree\/configurations/, | ||
url: solidus_paypal_braintree.list_configurations_path | ||
%> |
26 changes: 26 additions & 0 deletions
26
lib/views/backend/solidus_paypal_braintree/configurations/list.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<% content_for :page_title do %> | ||
<%= t(:title, scope: 'solidus_paypal_braintree.configurations') %> | ||
<% end %> | ||
|
||
<%= form_for :configurations, url: solidus_paypal_braintree.configurations_path do |f| %> | ||
<% @configurations.each do |config| %> | ||
<div class="row"> | ||
<fieldset> | ||
<h1><%= config.store.name %></h1> | ||
|
||
<%= f.fields_for 'configuration_fields[]', config do |c| %> | ||
<div class="field"> | ||
<%= c.label :paypal %> | ||
<%= c.check_box :paypal %> | ||
</div> | ||
<div class="field"> | ||
<%= c.label :apple_pay %> | ||
<%= c.check_box :apple_pay %> | ||
</div> | ||
<% end %> | ||
</fieldset> | ||
</div> | ||
<% end %> | ||
|
||
<%= submit_tag %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
spec/controllers/solidus_paypal_braintree/client_tokens_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
spec/controllers/solidus_paypal_braintree/configurations_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
require 'spec_helper' | ||
|
||
describe SolidusPaypalBraintree::ConfigurationsController, type: :controller do | ||
routes { SolidusPaypalBraintree::Engine.routes } | ||
|
||
let!(:store_1) { create :store } | ||
let!(:store_2) { create :store } | ||
let(:store_1_config) { store_1.braintree_configuration } | ||
let(:store_2_config) { store_2.braintree_configuration } | ||
|
||
stub_authorization! | ||
|
||
describe "GET #list" do | ||
subject { get :list } | ||
|
||
it "assigns all store's configurations as @configurations" do | ||
subject | ||
expect(assigns(:configurations)). | ||
to eq [store_1.braintree_configuration, store_2.braintree_configuration] | ||
end | ||
|
||
it "renders the correct view" do | ||
expect(subject).to render_template :list | ||
end | ||
end | ||
|
||
describe "POST #update" do | ||
let(:configurations_params) do | ||
{ | ||
configurations: { | ||
configuration_fields: { | ||
store_1_config.id.to_s => { paypal: true, apple_pay: true }, | ||
store_2_config.id.to_s => { paypal: true, apple_pay: false } | ||
} | ||
} | ||
} | ||
end | ||
|
||
subject { post :update, configurations_params } | ||
|
||
context "with valid parameters" do | ||
it "updates the configuration" do | ||
expect { subject }.to change { store_1_config.reload.paypal }. | ||
from(false).to(true) | ||
end | ||
|
||
it "displays a success message to the user" do | ||
subject | ||
expect(flash[:success]).to eq "Successfully updated Braintree configurations." | ||
end | ||
|
||
it "displays all configurations" do | ||
expect(subject).to redirect_to action: :list | ||
end | ||
end | ||
|
||
context "with invalid parameters" do | ||
before { allow(SolidusPaypalBraintree::Configuration).to receive(:update) { false } } | ||
|
||
it "displays an error message to the user" do | ||
subject | ||
expect(flash[:error]).to eq "An error occurred while updating Braintree configurations." | ||
end | ||
|
||
it "returns the user to the edit page" do | ||
expect(subject).to redirect_to action: :list | ||
end | ||
end | ||
end | ||
end |
2 changes: 2 additions & 0 deletions
2
spec/controllers/solidus_paypal_braintree/transactions_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters