diff options
35 files changed, 357 insertions, 210 deletions
@@ -78,4 +78,21 @@ CouchRest Model behaved strangely when using a model without a design block. So From that point on you should be able to use the standart persistance and querying methods such as create, find, destroy and so on. +## Writing Tests ## +### Locale + +The ApplicationController defines a before filter #set_locale that will set +the default_url_options to include the appropriate default {:locale => x} param. + +However, paths generated in tests don't use default_url_options. This can +create failures for certain nested routes unless you explicitly provide +:locale => nil to the path helper. This is not needed for actual path code in +the controllers or views, only when generating paths in tests. + +For example: + + test "robot" do + login_as @user + visit robot_path(@robot, :locale => nil) + end
\ No newline at end of file @@ -4,28 +4,26 @@ eval(File.read(File.dirname(__FILE__) + '/common_dependencies.rb')) eval(File.read(File.dirname(__FILE__) + '/ui_dependencies.rb')) # EITHER fetch all of the leap_web gems in one go -# gem 'leap_web' +# gem 'leap_web' # OR use the local versions for development instead: gem "leap_web_core", :path => 'core' gem 'leap_web_users', :path => 'users' gem 'leap_web_certs', :path => 'certs' gem 'leap_web_help', :path => 'help' -# gem 'leap_web_billing', :path => 'billing' # for now, this gem will be included for development and test environments only +gem 'leap_web_billing', :path => 'billing' + +gem 'http_accept_language' # To use debugger gem 'debugger', :platforms => :mri_19 # ruby 1.8 is not supported anymore # gem 'ruby-debug', :platforms => :mri_18 -group :development do - gem 'leap_web_billing', :path => 'billing' -end - group :test do gem 'fake_braintree', require: false gem 'capybara', require: false gem 'launchy' # so save_and_open_page works in integration tests - gem 'leap_web_billing', :path => 'billing' + gem 'phantomjs-binaries' end # unreleased so far ... but leap_web_certs need it diff --git a/Gemfile.lock b/Gemfile.lock index 918fdba..cc549ee 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -8,21 +8,21 @@ GIT PATH remote: billing specs: - leap_web_billing (0.2.8) + leap_web_billing (0.5.0.rc) braintree - leap_web_core (= 0.2.8) + leap_web_core (= 0.5.0.rc) PATH remote: certs specs: - leap_web_certs (0.2.8) + leap_web_certs (0.5.0.rc) certificate_authority (>= 0.2.0) - leap_web_core (= 0.2.8) + leap_web_core (= 0.5.0.rc) PATH remote: core specs: - leap_web_core (0.2.8) + leap_web_core (0.5.0.rc) couchrest (~> 1.1.3) couchrest_model (~> 2.0.0) couchrest_session_store (~> 0.2.4) @@ -32,14 +32,14 @@ PATH PATH remote: help specs: - leap_web_help (0.2.8) - leap_web_core (= 0.2.8) + leap_web_help (0.5.0.rc) + leap_web_core (= 0.5.0.rc) PATH remote: users specs: - leap_web_users (0.2.8) - leap_web_core (= 0.2.8) + leap_web_users (0.5.0.rc) + leap_web_core (= 0.5.0.rc) rails_warden ruby-srp (~> 0.2.1) @@ -47,12 +47,12 @@ GEM remote: https://rubygems.org/ specs: SyslogLogger (2.0) - actionmailer (3.2.15) - actionpack (= 3.2.15) + actionmailer (3.2.16) + actionpack (= 3.2.16) mail (~> 2.5.4) - actionpack (3.2.15) - activemodel (= 3.2.15) - activesupport (= 3.2.15) + actionpack (3.2.16) + activemodel (= 3.2.16) + activesupport (= 3.2.16) builder (~> 3.0.0) erubis (~> 2.7.0) journey (~> 1.0.4) @@ -60,29 +60,29 @@ GEM rack-cache (~> 1.2) rack-test (~> 0.6.1) sprockets (~> 2.2.1) - activemodel (3.2.15) - activesupport (= 3.2.15) + activemodel (3.2.16) + activesupport (= 3.2.16) builder (~> 3.0.0) - activerecord (3.2.15) - activemodel (= 3.2.15) - activesupport (= 3.2.15) + activerecord (3.2.16) + activemodel (= 3.2.16) + activesupport (= 3.2.16) arel (~> 3.0.2) tzinfo (~> 0.3.29) - activeresource (3.2.15) - activemodel (= 3.2.15) - activesupport (= 3.2.15) - activesupport (3.2.15) + activeresource (3.2.16) + activemodel (= 3.2.16) + activesupport (= 3.2.16) + activesupport (3.2.16) i18n (~> 0.6, >= 0.6.4) multi_json (~> 1.0) addressable (2.3.5) - arel (3.0.2) + arel (3.0.3) bootstrap-sass (2.1.1.0) bootswatch-rails (0.5.0) railties (>= 3.1) - braintree (2.25.0) + braintree (2.27.1) builder (>= 2.0.0) builder (3.0.4) - capybara (2.1.0) + capybara (2.2.0) mime-types (>= 1.16) nokogiri (>= 1.3.3) rack (>= 1.0.0) @@ -92,7 +92,7 @@ GEM client_side_validations-simple_form (2.1.0) client_side_validations (~> 3.2.5) simple_form (~> 2.1.0) - cliver (0.2.2) + cliver (0.3.2) coffee-rails (3.2.2) coffee-script (>= 2.2.0) railties (~> 3.2.0) @@ -115,19 +115,19 @@ GEM couchrest couchrest_model daemons (1.1.9) - debugger (1.6.2) + debugger (1.6.3) columnize (>= 0.3.1) debugger-linecache (~> 1.2.0) - debugger-ruby_core_source (~> 1.2.3) + debugger-ruby_core_source (~> 1.2.4) debugger-linecache (1.2.0) debugger-ruby_core_source (1.2.4) erubis (2.7.0) eventmachine (1.0.3) execjs (2.0.2) - factory_girl (4.2.0) + factory_girl (4.3.0) activesupport (>= 3.0.0) - factory_girl_rails (4.2.1) - factory_girl (~> 4.2.0) + factory_girl_rails (4.3.0) + factory_girl (~> 4.3.0) railties (>= 3.0.0) fake_braintree (0.4) activesupport @@ -138,6 +138,7 @@ GEM thin faker (1.2.0) i18n (~> 0.5) + ffi (1.9.3) haml (3.1.8) haml-rails (0.3.5) actionpack (>= 3.1, < 4.1) @@ -145,6 +146,7 @@ GEM haml (~> 3.1) railties (>= 3.1, < 4.1) hike (1.2.3) + http_accept_language (2.0.0) i18n (0.6.9) journey (1.0.4) jquery-rails (3.0.4) @@ -155,7 +157,7 @@ GEM actionpack (>= 3.0.0) activesupport (>= 3.0.0) railties (>= 3.0.0) - launchy (2.3.0) + launchy (2.4.2) addressable (~> 2.3) libv8 (3.3.10.4) mail (2.5.4) @@ -163,15 +165,17 @@ GEM treetop (~> 1.4.8) metaclass (0.0.1) mime-types (1.25.1) - mini_portile (0.5.1) + mini_portile (0.5.2) mocha (0.13.3) metaclass (~> 0.0.1) multi_json (1.8.2) - nokogiri (1.6.0) + nokogiri (1.6.1) mini_portile (~> 0.5.0) - poltergeist (1.4.1) - capybara (~> 2.1.0) - cliver (~> 0.2.1) + phantomjs-binaries (1.9.2.3) + sys-uname (= 0.9.0) + poltergeist (1.5.0) + capybara (~> 2.1) + cliver (~> 0.3.1) multi_json (~> 1.0) websocket-driver (>= 0.2.0) polyglot (0.3.3) @@ -180,47 +184,47 @@ GEM rack (1.4.5) rack-cache (1.2) rack (>= 0.4) - rack-protection (1.5.0) + rack-protection (1.5.1) rack rack-ssl (1.3.3) rack rack-test (0.6.2) rack (>= 1.0) - rails (3.2.15) - actionmailer (= 3.2.15) - actionpack (= 3.2.15) - activerecord (= 3.2.15) - activeresource (= 3.2.15) - activesupport (= 3.2.15) + rails (3.2.16) + actionmailer (= 3.2.16) + actionpack (= 3.2.16) + activerecord (= 3.2.16) + activeresource (= 3.2.16) + activesupport (= 3.2.16) bundler (~> 1.0) - railties (= 3.2.15) + railties (= 3.2.16) rails-i18n (3.0.0) i18n (~> 0.5) rails (>= 3.0.0, < 4.0.0) rails_warden (0.5.7) warden (>= 1.0.0) - railties (3.2.15) - actionpack (= 3.2.15) - activesupport (= 3.2.15) + railties (3.2.16) + actionpack (= 3.2.16) + activesupport (= 3.2.16) rack-ssl (~> 1.3.2) rake (>= 0.8.7) rdoc (~> 3.4) thor (>= 0.14.6, < 2.0) - rake (10.1.0) + rake (10.1.1) rdoc (3.12.2) json (~> 1.4) rest-client (1.6.7) mime-types (>= 1.16) ruby-srp (0.2.1) - sass (3.2.12) + sass (3.2.13) sass-rails (3.2.6) railties (~> 3.2.0) sass (>= 3.1.10) tilt (~> 1.3) - simple_form (2.1.0) + simple_form (2.1.1) actionpack (~> 3.0) activemodel (~> 3.0) - sinatra (1.4.3) + sinatra (1.4.4) rack (~> 1.4) rack-protection (~> 1.4) tilt (~> 1.3, >= 1.3.4) @@ -229,11 +233,13 @@ GEM multi_json (~> 1.0) rack (~> 1.0) tilt (~> 1.1, != 1.3.0) + sys-uname (0.9.0) + ffi (>= 1.0.0) therubyracer (0.10.2) libv8 (~> 3.3.10) - thin (1.5.1) + thin (1.6.1) daemons (>= 1.0.9) - eventmachine (>= 0.12.6) + eventmachine (>= 1.0.0) rack (>= 1.0.0) thor (0.18.1) tilt (1.4.1) @@ -246,7 +252,7 @@ GEM multi_json (~> 1.3) warden (1.2.3) rack (>= 1.0) - websocket-driver (0.3.0) + websocket-driver (0.3.1) xpath (2.0.0) nokogiri (~> 1.3) @@ -268,6 +274,7 @@ DEPENDENCIES faker haml (~> 3.1.7) haml-rails (~> 0.3.4) + http_accept_language jquery-rails kaminari (= 0.13.0) launchy @@ -277,6 +284,7 @@ DEPENDENCIES leap_web_help! leap_web_users! mocha (~> 0.13.0) + phantomjs-binaries poltergeist quiet_assets rails-i18n @@ -104,3 +104,5 @@ To run all tests To run an individual test: rake test TEST=certs/test/unit/client_certificate_test.rb + or + ruby -Itest certs/test/unit/client_certificate_test.rb
\ No newline at end of file diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index de8d06b..35d6cb4 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,13 +1,14 @@ class ApplicationController < ActionController::Base protect_from_forgery + before_filter :set_locale before_filter :no_cache_header before_filter :no_frame_header + before_filter :language_header ActiveSupport.run_load_hooks(:application_controller, self) protected - rescue_from StandardError do |e| respond_to do |format| format.json { render_json_error(e) } @@ -32,13 +33,59 @@ class ApplicationController < ActionController::Base end helper_method :bold + ## + ## LOCALE + ## + # - # we want to prevent the browser from caching anything, just to be safe. + # URL paths for which we don't enforce the locale as the prefix of the path. # - def no_cache_header - response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" - response.headers["Pragma"] = "no-cache" - response.headers["Expires"] = "0" + NON_LOCALE_PATHS = /^\/(assets|webfinger|.well-known|rails|key|[0-9]+)($|\/)/ + + # + # Before filter to set the current locale. Possible outcomes: + # + # (a) do nothing for certain routes and requests. + # (b) if path already starts with locale, set I18n.locale and default_url_options. + # (c) otherwise, redirect so that path starts with locale. + # + # If the locale happens to be the default local, no paths are prefixed with the locale. + # + def set_locale + if request.method == "GET" && request.format == 'text/html' && request.path !~ NON_LOCALE_PATHS + if params[:locale] && LOCALES_STRING.include?(params[:locale]) + I18n.locale = params[:locale] + update_default_url_options + else + I18n.locale = http_accept_language.compatible_language_from(I18n.available_locales) || I18n.default_locale + update_default_url_options + if I18n.locale != I18n.default_locale + redirect_to url_for(params.merge(:locale => I18n.locale)) + end + end + else + update_default_url_options + end + end + + def update_default_url_options + if I18n.locale != I18n.default_locale + self.default_url_options[:locale] = I18n.locale + else + self.default_url_options[:locale] = nil + end + end + + ## + ## HTTP HEADERS + ## These are in individual helpers so that controllers can disable them if needed. + ## + + # + # Not necessary, but kind to let the browser know the current locale. + # + def language_header + response.headers["Content-Language"] = I18n.locale.to_s end # @@ -48,4 +95,13 @@ class ApplicationController < ActionController::Base response.headers["X-Frame-Options"] = "DENY" end + # + # we want to prevent the browser from caching anything, just to be safe. + # + def no_cache_header + response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" + response.headers["Pragma"] = "no-cache" + response.headers["Expires"] = "0" + end + end diff --git a/app/views/layouts/_navigation.html.haml b/app/views/layouts/_navigation.html.haml index 6de567a..b81c43d 100644 --- a/app/views/layouts/_navigation.html.haml +++ b/app/views/layouts/_navigation.html.haml @@ -1,7 +1,7 @@ %ul.nav.sidenav - = link_to_navigation t(:overview), @user, :active => controller?(:overviews) - = link_to_navigation t(:account_settings), edit_user_path(@user), :active => controller?(:users) + = link_to_navigation t(:overview), @user, :active => (controller?(:users) && action?(:show)) + = link_to_navigation t(:account_settings), edit_user_path(@user), :active => (controller?(:users) && !action?(:show)) - # will want link for identity settings = link_to_navigation t(:support_tickets), auto_tickets_path, :active => controller?(:tickets) - = link_to_navigation t(:billing_settings), billing_top_link(@user), :active => controller?(:customer, :payments, :subscriptions, :credit_card_info) if APP_CONFIG[:payment].present? + = link_to_navigation t(:billing_settings), billing_top_link(@user), :active => controller?(:customer, :payments, :subscriptions, :credit_card_info) if APP_CONFIG[:billing] = link_to_navigation t(:logout), logout_path, :method => :delete diff --git a/billing/README.md b/billing/README.md new file mode 100644 index 0000000..3ef6153 --- /dev/null +++ b/billing/README.md @@ -0,0 +1,49 @@ +Billing Engine +==================== + +Currently, this engine support billing via Braintree. More backends to come later. + +Configuration +---------------------------------- + +Start with a sandbox account, which you can get here: https://www.braintreepayments.com/get-started + +Once you have registered for the sandbox, logging in will show you three important variables you will need to configure: + +* merchantId +* publicKey +* privatekey + +To configure the billing engine, edit `config/config.yaml` like so: + + production: (or "development", as you prefer) + billing: + braintree: + environment: sandbox + merchant_id: Ohp2aijaaqu6oJ4w + public_key: ahnar0UwLahwe6Ce + private_key: aemie2Geohgah2EaOad9DeeruW4Iegh4 + +If deploying via puppet, the same data in webapp.json would like this: + + "billing": { + "braintree": { + "environment": "sandbox", + "merchant_id": "Ohp2aijaaqu6oJ4w", + "public_key": "ahnar0UwLahwe6Ce", + "private_key": "aemie2Geohgah2EaOad9DeeruW4Iegh4" + } + } + +Now, you should be able to add charges to your own sandbox when you run the webapp. + +The acceptable values for `billing.braintree.environment` are: `development`, `qa`, `sandbox`, or `production`. + +Plans +-------------------------------- + +You also will want to add a Plan to your Sandbox. Within the Braintree Sandbox, navigate to 'Recurring Billing' -> 'Plans'. From here, you can add a new Plan. The values of the test plan are not important, but the ID will be displayed, so should pick something descriptive. + +Here are credit cared numbers to try in the Sandbox: + +https://www.braintreepayments.com/docs/ruby/reference/sandbox
\ No newline at end of file diff --git a/billing/README.rdoc b/billing/README.rdoc deleted file mode 100644 index 30ca0d6..0000000 --- a/billing/README.rdoc +++ /dev/null @@ -1,25 +0,0 @@ -= Billing - -This project rocks and uses MIT-LICENSE. - -The gem leap_web_billing will need to be included in whatever environment you are running, and billing will also need to be included in the configuration for that environment. You can set billing to be included in config/defaults.yml, by making sure the payment key is set to an array including billing (by default it will be set for the test environment, so you can look at that.) - -To set up your own Braintree Sandbox, create an account at: -https://www.braintreepayments.com/get-started - -Login to the Braintree Sandbox. -In the top right, navigate to your username, and then 'My User' -> 'API Keys' - -Click the button to generate a new API key, and then click the 'View' link to the right of the key. - -There is a section to copy a snippet of code. The simplest way to get this working is to select 'Ruby' in the dropdown, and then the button to the right to copy this code to your clipboard, and then paste the contents of the clipboard into billing/config/initializers/braintree.rb -However, you should not check the private key into version control, so you should not check in this file. -The better way to do this is to leave billing/config/initializers/braintree.rb as is, and instead set the braintree variables for the appropriate environment in config/config.yml, which is excluded from version control. - -Now, you should be able to add charges to your own Sandbox when you run the webapp locally. - -You also will want to add a Plan to your Sandbox. Within the Braintree Sandbox, navigate to 'Recurring Billing' -> 'Plans'. From here, you can add a new Plan. The values of the test plan are not important, but the ID will be displayed, so should pick something descriptive. - -Here are credit cared numbers to try in the Sandbox: - -https://www.braintreepayments.com/docs/ruby/reference/sandbox
\ No newline at end of file diff --git a/billing/config/initializers/braintree.rb b/billing/config/initializers/braintree.rb index d6ae565..c0c89e2 100644 --- a/billing/config/initializers/braintree.rb +++ b/billing/config/initializers/braintree.rb @@ -1,16 +1,31 @@ -require 'braintree_test_app' - -Braintree::Configuration.logger = Logger.new('log/braintree.log') +# +# set logger +# +if APP_CONFIG[:logfile].blank? + require 'syslog/logger' + Braintree::Configuration.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new('webapp')) +else + Braintree::Configuration.logger = Logger.new('log/braintree.log') +end +# # we use fake braintree in tests +# if Rails.env.test? + require 'braintree_test_app' Rails.application.config.middleware.use BraintreeTestApp end +# # You can set these per environment in config/config.yml: -if braintree_conf = APP_CONFIG[:braintree] - Braintree::Configuration.environment = braintree_conf[:environment] - Braintree::Configuration.merchant_id = braintree_conf[:merchant_id] - Braintree::Configuration.public_key = braintree_conf[:public_key] - Braintree::Configuration.private_key = braintree_conf[:private_key] +# +# Environment must be one of: :development, :qa, :sandbox, :production +# +if billing = APP_CONFIG[:billing] + if braintree = billing[:braintree] + Braintree::Configuration.environment = braintree[:environment].downcase.to_sym + Braintree::Configuration.merchant_id = braintree[:merchant_id] + Braintree::Configuration.public_key = braintree[:public_key] + Braintree::Configuration.private_key = braintree[:private_key] + end end diff --git a/billing/config/routes.rb b/billing/config/routes.rb index dbdc24b..7263dff 100644 --- a/billing/config/routes.rb +++ b/billing/config/routes.rb @@ -1,24 +1,25 @@ Rails.application.routes.draw do - match 'payments/new' => 'payments#new', :as => :new_payment - match 'payments/confirm' => 'payments#confirm', :as => :confirm_payment - resources :users do - resources :payments, :only => [:index] - resources :subscriptions, :only => [:index, :show, :destroy] - end - - resources :customer, :only => [:new, :edit] - resources :credit_card_info, :only => [:edit] + scope "(:locale)", :locale => MATCH_LOCALE do + match 'payments/new' => 'payments#new', :as => :new_payment + match 'payments/confirm' => 'payments#confirm', :as => :confirm_payment + resources :users do + resources :payments, :only => [:index] + resources :subscriptions, :only => [:index, :show, :destroy] + end - match 'customer/confirm/' => 'customer#confirm', :as => :confirm_customer - match 'customer/show/:id' => 'customer#show', :as => :show_customer - match 'credit_card_info/confirm' => 'credit_card_info#confirm', :as => :confirm_credit_card_info + resources :customer, :only => [:new, :edit] + resources :credit_card_info, :only => [:edit] - resources :subscriptions, :only => [:new, :create, :update] # index, show & destroy are within users path - match 'billing_admin' => 'billing_admin#show', :as => :billing_admin + match 'customer/confirm/' => 'customer#confirm', :as => :confirm_customer + match 'customer/show/:id' => 'customer#show', :as => :show_customer + match 'credit_card_info/confirm' => 'credit_card_info#confirm', :as => :confirm_credit_card_info - #match 'transactions/:product_id/new' => 'transactions#new', :as => :new_transaction - #match 'transactions/confirm/:product_id' => 'transactions#confirm', :as => :confirm_transaction + resources :subscriptions, :only => [:new, :create, :update] # index, show & destroy are within users path + match 'billing_admin' => 'billing_admin#show', :as => :billing_admin + #match 'transactions/:product_id/new' => 'transactions#new', :as => :new_transaction + #match 'transactions/confirm/:product_id' => 'transactions#confirm', :as => :confirm_transaction + end end diff --git a/billing/leap_web_billing.gemspec b/billing/leap_web_billing.gemspec index 17f7f92..94d92e0 100644 --- a/billing/leap_web_billing.gemspec +++ b/billing/leap_web_billing.gemspec @@ -12,7 +12,7 @@ Gem::Specification.new do |s| s.summary = "Billing for LeapWeb" s.description = "Billing System for a Leap provider" - s.files = Dir["{app,config,db,lib}/**/*"] + ["Rakefile", "Readme.md"] + s.files = Dir["{app,config,db,lib}/**/*"] + ["Rakefile", "README.md"] s.test_files = Dir["test/**/*"] s.add_dependency "leap_web_core", LeapWeb::VERSION diff --git a/billing/test/integration/subscription_test.rb b/billing/test/integration/subscription_test.rb index 6356177..b95bfac 100644 --- a/billing/test/integration/subscription_test.rb +++ b/billing/test/integration/subscription_test.rb @@ -29,7 +29,7 @@ class SubscriptionTest < ActionDispatch::IntegrationTest login_as @admin @customer.stubs(:subscriptions).returns([@subscription]) @subscription.stubs(:balance).returns 0 - visit user_subscriptions_path(@customer.user_id) + visit user_subscriptions_path(@customer.user_id, :locale => nil) assert page.has_content?("Subscriptions") assert page.has_content?("Status: Active") page.save_screenshot('/tmp/subscriptions.png') @@ -1,4 +1,7 @@ # This file is used by Rack-based servers to start the application. +require 'http_accept_language' +use HttpAcceptLanguage::Middleware + require ::File.expand_path('../config/environment', __FILE__) run LeapWeb::Application diff --git a/config/defaults.yml b/config/defaults.yml index 260915e..98dc334 100644 --- a/config/defaults.yml +++ b/config/defaults.yml @@ -44,7 +44,10 @@ common: &common # see /users/app/views/users/_edit.html.haml for a list. user_actions: ['destroy_account'] admin_actions: ['change_pgp_key', 'change_service_level', 'destroy_account'] - + billing: ~ + default_locale: :en + available_locales: + - :en service_levels: &service_levels service_levels: 0: @@ -75,7 +78,6 @@ development: admins: [blue, admin, admin2] domain: example.org secret_token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' - payment: [] reraise_errors: true test: @@ -87,8 +89,12 @@ test: admins: [admin, admin2] domain: test.me secret_token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' - payment: [billing] reraise_errors: true + billing: {} + default_locale: :en + available_locales: + - :en + - :de production: <<: *downloads @@ -96,5 +102,4 @@ production: <<: *common admins: [] domain: example.net - payment: [] # logfile: /path/to/your/logs diff --git a/config/initializers/i18n.rb b/config/initializers/i18n.rb index 574d169..c277a22 100644 --- a/config/initializers/i18n.rb +++ b/config/initializers/i18n.rb @@ -1,2 +1,10 @@ +I18n.enforce_available_locales = true +I18n.available_locales = APP_CONFIG[:available_locales] +I18n.default_locale = APP_CONFIG[:default_locale] -I18n.available_locales = ['en'] +# Used to match locales route prefixes +MATCH_LOCALE = /(#{I18n.available_locales.join('|')})/ + +# I18n.available_locales is always an array of symbols, but for comparison with +# params we need it to be an array of strings. +LOCALES_STRING = I18n.available_locales.map(&:to_s) diff --git a/config/routes.rb b/config/routes.rb index 3b29251..12bfe93 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,56 +1,9 @@ LeapWeb::Application.routes.draw do - root :to => "home#index" - - # The priority is based upon order of creation: - # first created -> highest priority. - - # Sample of regular route: - # match 'products/:id' => 'catalog#view' - # Keep in mind you can assign values other than :controller and :action - - # Sample of named route: - # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase - # This route can be invoked with purchase_url(:id => product.id) - - # Sample resource route (maps HTTP verbs to controller actions automatically): - # resources :products - - # Sample resource route with options: - # resources :products do - # member do - # get 'short' - # post 'toggle' - # end # - # collection do - # get 'sold' - # end - # end - - # Sample resource route with sub-resources: - # resources :products do - # resources :comments, :sales - # resource :seller - # end - - # Sample resource route with more complex sub-resources - # resources :products do - # resources :comments - # resources :sales do - # get 'recent', :on => :collection - # end - # end - - # Sample resource route within a namespace: - # namespace :admin do - # # Directs /admin/products/* to Admin::ProductsController - # # (app/controllers/admin/products_controller.rb) - # resources :products - # end - - # See how all your routes lay out with "rake routes" + # Please do not use root_path or root_url. Use home_path and home_url instead, + # so that the path will be correctly prefixed with the locale. + # + root :to => "home#index" + get '(:locale)' => 'home#index', :locale => MATCH_LOCALE, :as => 'home' - # This is a legacy wild controller route that's not recommended for RESTful applications. - # Note: This route will make all actions in every controller accessible via GET requests. - # match ':controller(/:action(/:id))(.:format)' end diff --git a/core/app/views/common/_home_page_buttons.html.haml b/core/app/views/common/_home_page_buttons.html.haml index 8aff51e..c9ea7a2 100644 --- a/core/app/views/common/_home_page_buttons.html.haml +++ b/core/app/views/common/_home_page_buttons.html.haml @@ -16,8 +16,8 @@ %span.link= link_to(icon('ok-sign', icon_color) + t(:login), login_path, :class => 'btn') %span.info= t(:login_info) .signup.span4 - %span.link= link_to(icon('user', icon_color) + t(:signup), new_user_path, :class => 'btn') + %span.link= link_to(icon('user', icon_color) + t(:signup), signup_path, :class => 'btn') %span.info= t(:signup_info) .help.span4 - %span.link= link_to(icon('question-sign', icon_color) + t(:get_help), "/tickets/new", :class => 'btn') + %span.link= link_to(icon('question-sign', icon_color) + t(:get_help), new_ticket_path, :class => 'btn') %span.info= t(:help_info) diff --git a/core/config/routes.rb b/core/config/routes.rb index 63f644c..1daf9a4 100644 --- a/core/config/routes.rb +++ b/core/config/routes.rb @@ -1,3 +1,2 @@ Rails.application.routes.draw do - root :to => "home#index" end diff --git a/help/app/views/tickets/new.html.haml b/help/app/views/tickets/new.html.haml index 393e5d6..8f217a5 100644 --- a/help/app/views/tickets/new.html.haml +++ b/help/app/views/tickets/new.html.haml @@ -27,4 +27,4 @@ - if logged_in? = link_to t(:cancel), auto_tickets_path, :class => :btn - else - = link_to t(:cancel), root_path, :class => 'btn'
\ No newline at end of file + = link_to t(:cancel), home_path, :class => 'btn'
\ No newline at end of file diff --git a/help/config/routes.rb b/help/config/routes.rb index 8f3241c..23e0c11 100644 --- a/help/config/routes.rb +++ b/help/config/routes.rb @@ -1,6 +1,8 @@ Rails.application.routes.draw do - resources :tickets, :except => :edit - resources :users do + scope "(:locale)", :locale => MATCH_LOCALE do resources :tickets, :except => :edit + resources :users do + resources :tickets, :except => :edit + end end end diff --git a/help/test/functional/tickets_controller_test.rb b/help/test/functional/tickets_controller_test.rb index 0f56e6e..2b30f66 100644 --- a/help/test/functional/tickets_controller_test.rb +++ b/help/test/functional/tickets_controller_test.rb @@ -49,7 +49,7 @@ class TicketsControllerTest < ActionController::TestCase login get :show, :id => ticket.id assert_response :redirect - assert_redirected_to root_url + assert_redirected_to home_url end test "should create unauthenticated ticket" do diff --git a/lib/leap_web/version.rb b/lib/leap_web/version.rb index 983e3ad..6915930 100644 --- a/lib/leap_web/version.rb +++ b/lib/leap_web/version.rb @@ -1,3 +1,3 @@ module LeapWeb - VERSION = "0.2.8" unless defined?(LeapWeb::VERSION) + VERSION = "0.5.0.rc" unless defined?(LeapWeb::VERSION) end diff --git a/test/integration/locale_path_test.rb b/test/integration/locale_path_test.rb new file mode 100644 index 0000000..2c43ab2 --- /dev/null +++ b/test/integration/locale_path_test.rb @@ -0,0 +1,51 @@ +require 'test_helper' + +# +# Test how we handle redirections and locales. +# +# The basic rules are: +# +# (1) If the browser header Accept-Language matches default locale, then don't do a locale prefix. +# (2) If browser locale is supported in available_locales, but is not the default, then redirect. +# (3) If browser locale is not in available_locales, use the default locale with no prefix. +# +# Settings in defaults.yml +# +# default_locale: :en +# available_locales: +# - :en +# - :de +# +# NOTE: Although the browser sends the header Accept-Language, this is parsed by +# ruby as HTTP_ACCEPT_LANGUAGE +# + +class LocalePathTest < ActionDispatch::IntegrationTest + test "redirect if accept-language is not default locale" do + get_via_redirect '/', {}, 'HTTP_ACCEPT_LANGUAGE' => 'de' + assert_equal '/de', path + assert_equal({:locale => :de}, @controller.default_url_options) + end + + test "no locale prefix" do + get_via_redirect '/', {}, 'HTTP_ACCEPT_LANGUAGE' => 'en' + assert_equal '/', path + assert_equal({:locale => nil}, @controller.default_url_options) + + get_via_redirect '/', {}, 'HTTP_ACCEPT_LANGUAGE' => 'pt' + assert_equal '/', path + assert_equal({:locale => nil}, @controller.default_url_options) + end + + test "no redirect if locale explicit" do + get_via_redirect '/de', {}, 'HTTP_ACCEPT_LANGUAGE' => 'en' + assert_equal '/de', path + assert_equal({:locale => :de}, @controller.default_url_options) + end + + test "strip prefix from url options if locale is default" do + get_via_redirect '/en', {}, 'HTTP_ACCEPT_LANGUAGE' => 'en' + assert_equal '/en', path + assert_equal({:locale => nil}, @controller.default_url_options) + end +end
\ No newline at end of file diff --git a/users/app/controllers/controller_extension/authentication.rb b/users/app/controllers/controller_extension/authentication.rb index dca3664..d831fbe 100644 --- a/users/app/controllers/controller_extension/authentication.rb +++ b/users/app/controllers/controller_extension/authentication.rb @@ -23,7 +23,7 @@ module ControllerExtension::Authentication respond_to do |format| format.html do if logged_in? - redirect_to root_url, :alert => t(:not_authorized) + redirect_to home_url, :alert => t(:not_authorized) else redirect_to login_url, :alert => t(:not_authorized_login) end diff --git a/users/app/controllers/sessions_controller.rb b/users/app/controllers/sessions_controller.rb index ca228c2..0195f30 100644 --- a/users/app/controllers/sessions_controller.rb +++ b/users/app/controllers/sessions_controller.rb @@ -1,7 +1,7 @@ class SessionsController < ApplicationController def new - redirect_to root_path if logged_in? + redirect_to home_url if logged_in? @session = Session.new if authentication_errors @errors = authentication_errors @@ -11,7 +11,7 @@ class SessionsController < ApplicationController def destroy logout - redirect_to root_path + redirect_to home_url end # diff --git a/users/app/controllers/users_controller.rb b/users/app/controllers/users_controller.rb index 0b32ec7..a874772 100644 --- a/users/app/controllers/users_controller.rb +++ b/users/app/controllers/users_controller.rb @@ -61,7 +61,7 @@ class UsersController < UsersBaseController else # let's remove the invalid session logout - redirect_to root_url + redirect_to home_url end end diff --git a/users/app/views/sessions/new.html.haml b/users/app/views/sessions/new.html.haml index 0939e00..771dc97 100644 --- a/users/app/views/sessions/new.html.haml +++ b/users/app/views/sessions/new.html.haml @@ -7,4 +7,4 @@ = f.input :password, :required => false, :input_html => { :id => :srp_password } .form-actions = f.button :submit, :value => t(:login), :class => 'btn-primary' - = link_to t(:cancel), root_path, :class => 'btn' + = link_to t(:cancel), home_path, :class => 'btn' diff --git a/users/app/views/users/new.html.haml b/users/app/views/users/new.html.haml index f8d14b5..aecf831 100644 --- a/users/app/views/users/new.html.haml +++ b/users/app/views/users/new.html.haml @@ -15,5 +15,5 @@ = f.input :password_confirmation, :required => false, :validate => true, :input_html => { :id => :srp_password_confirmation } .form-actions = f.button :submit, :value => t(:signup), :class => 'btn btn-primary' - = link_to t(:cancel), root_url, :class => 'btn' + = link_to t(:cancel), home_path, :class => 'btn' diff --git a/users/app/views/users/show.html.haml b/users/app/views/users/show.html.haml index eace240..c587017 100644 --- a/users/app/views/users/show.html.haml +++ b/users/app/views/users/show.html.haml @@ -19,7 +19,7 @@ %li= icon('user') + link_to(t(:overview_account), edit_user_path(@user)) - # %li= icon('envelope') + link_to(t(:overview_email), {insert path for user identities, presuambly} %li= icon('question-sign') + link_to(t(:overview_tickets), user_tickets_path(@user)) - %li= icon('shopping-cart') + link_to(t(:overview_billing), billing_top_link(@user)) if APP_CONFIG[:payment].present? + %li= icon('shopping-cart') + link_to(t(:overview_billing), billing_top_link(@user)) if APP_CONFIG[:billing] .container-fluid .row-fluid = home_page_buttons(true)
\ No newline at end of file diff --git a/users/config/locales/en.yml b/users/config/locales/en.yml index 1b5dd5e..934fcee 100644 --- a/users/config/locales/en.yml +++ b/users/config/locales/en.yml @@ -46,7 +46,7 @@ en: overview_intro: "From this user control panel, you can:" overview_tickets: "Create and check support tickets." overview_email: "Modify email settings." - overview_account: "Change your password or delete your account." + overview_account: "Delete your account." # # rails diff --git a/users/config/routes.rb b/users/config/routes.rb index de2ff37..736b283 100644 --- a/users/config/routes.rb +++ b/users/config/routes.rb @@ -8,15 +8,17 @@ Rails.application.routes.draw do resources :users, :only => [:create, :update, :destroy, :index] end - get "login" => "sessions#new", :as => "login" - delete "logout" => "sessions#destroy", :as => "logout" + scope "(:locale)", :locale => MATCH_LOCALE do + get "login" => "sessions#new", :as => "login" + delete "logout" => "sessions#destroy", :as => "logout" - get "signup" => "users#new", :as => "signup" - resources :users, :except => [:create, :update] do - # resource :email_settings, :only => [:edit, :update] - # resources :email_aliases, :only => [:destroy], :id => /.*/ - post 'deactivate', on: :member - post 'enable', on: :member + get "signup" => "users#new", :as => "signup" + resources :users, :except => [:create, :update] do + # resource :email_settings, :only => [:edit, :update] + # resources :email_aliases, :only => [:destroy], :id => /.*/ + post 'deactivate', on: :member + post 'enable', on: :member + end end get "/.well-known/host-meta" => 'webfinger#host_meta' diff --git a/users/test/functional/sessions_controller_test.rb b/users/test/functional/sessions_controller_test.rb index 8b49005..fe7903f 100644 --- a/users/test/functional/sessions_controller_test.rb +++ b/users/test/functional/sessions_controller_test.rb @@ -17,11 +17,11 @@ class SessionsControllerTest < ActionController::TestCase assert_template "sessions/new" end - test "redirect to root_url if logged in" do + test "redirect to home_url if logged in" do login get :new assert_response :redirect - assert_redirected_to root_url + assert_redirected_to home_url end test "renders json" do @@ -53,7 +53,7 @@ class SessionsControllerTest < ActionController::TestCase expect_logout delete :destroy assert_response :redirect - assert_redirected_to root_url + assert_redirected_to home_url end end diff --git a/users/test/functional/users_controller_test.rb b/users/test/functional/users_controller_test.rb index 9c5f8d9..002a011 100644 --- a/users/test/functional/users_controller_test.rb +++ b/users/test/functional/users_controller_test.rb @@ -103,7 +103,7 @@ class UsersControllerTest < ActionController::TestCase delete :destroy, :id => @current_user.id assert_response :redirect - assert_redirected_to root_path + assert_redirected_to home_path end test "non-admin can't destroy user" do diff --git a/users/test/integration/browser/account_test.rb b/users/test/integration/browser/account_test.rb index 4cefe35..3785b72 100644 --- a/users/test/integration/browser/account_test.rb +++ b/users/test/integration/browser/account_test.rb @@ -14,7 +14,7 @@ class AccountTest < BrowserIntegrationTest username, password = submit_signup assert page.has_content?("Welcome #{username}") click_on 'Logout' - assert page.has_content?("Sign Up") + assert page.has_content?("Log In") assert_equal '/', current_path assert user = User.find_by_login(username) user.account.destroy @@ -25,6 +25,9 @@ class AccountTest < BrowserIntegrationTest click_on 'Logout' attempt_login(username, password) assert page.has_content?("Welcome #{username}") + within('.sidenav li.active') do + assert page.has_content?("Overview") + end User.find_by_login(username).account.destroy end diff --git a/users/test/support/auth_test_helper.rb b/users/test/support/auth_test_helper.rb index 50e9453..57f9f9b 100644 --- a/users/test/support/auth_test_helper.rb +++ b/users/test/support/auth_test_helper.rb @@ -27,7 +27,7 @@ module AuthTestHelper else if logged_in assert_equal({:alert => I18n.t(:not_authorized)}, flash.to_hash) - assert_redirected_to root_url + assert_redirected_to home_url else assert_equal({:alert => I18n.t(:not_authorized_login)}, flash.to_hash) assert_redirected_to login_url |