diff --git a/Gemfile b/Gemfile index 16d2b5c7..7341cf00 100644 --- a/Gemfile +++ b/Gemfile @@ -36,7 +36,7 @@ gem 'simple-navigation-bootstrap' gem 'meta_search' gem 'acts_as_versioned', git: 'git://github.com/technoweenie/acts_as_versioned.git' # Use this instead of rubygem gem 'acts_as_tree' -gem 'acts_as_configurable', git: 'git://github.com/bwalding/acts_as_configurable.git' +gem "rails-settings-cached", "0.2.4" gem 'resque' gem 'whenever', require: false # For defining cronjobs, see config/schedule.rb @@ -51,7 +51,8 @@ group :development do # Better error output gem 'better_errors' gem 'binding_of_caller' - + # gem "rails-i18n-debug" + # Re-enable rails benchmarker/profiler gem 'ruby-prof' gem 'test-unit' @@ -69,3 +70,7 @@ group :development do # Avoid having content-length warnings gem 'thin' end + +# Gems left for backwards compatibility +gem 'acts_as_configurable', git: 'git://github.com/bwalding/acts_as_configurable.git' # user settings migration needs it + diff --git a/Gemfile.lock b/Gemfile.lock index 77f4c623..d64eec91 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -183,6 +183,8 @@ GEM activesupport (= 3.2.13) bundler (~> 1.0) railties (= 3.2.13) + rails-settings-cached (0.2.4) + rails (>= 3.0.0) railties (3.2.13) actionpack (= 3.2.13) activesupport (= 3.2.13) @@ -299,6 +301,7 @@ DEPENDENCIES prawn quiet_assets rails (~> 3.2.9) + rails-settings-cached (= 0.2.4) resque ruby-prof sass-rails (~> 3.2.3) diff --git a/app/assets/stylesheets/bootstrap_and_overrides.css.less b/app/assets/stylesheets/bootstrap_and_overrides.css.less index 2cdb337e..408f6c65 100644 --- a/app/assets/stylesheets/bootstrap_and_overrides.css.less +++ b/app/assets/stylesheets/bootstrap_and_overrides.css.less @@ -219,3 +219,21 @@ tr.unavailable { dt { width: 160px; } dd { margin-left: 170px; } } + +.settings { + .settings-group { + margin-bottom: 10px; + .control-label { + margin: 5px 0 0 0; + } + } + .control-group { + margin-bottom: 5px; + } + .control-group.h_wrapper { + margin-bottom: 5px; + } + .control-group.select { + margin-bottom: 15px + } +} diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 29b752a0..96b2e510 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,10 +1,13 @@ # encoding: utf-8 class ApplicationController < ActionController::Base + include Foodsoft::ControllerExtensions::Locale + helper_method :available_locales protect_from_forgery - before_filter :select_language, :select_foodcoop, :authenticate, :store_controller, :items_per_page, :set_redirect_to + before_filter :select_foodcoop, :authenticate, :store_controller, :items_per_page, :set_redirect_to after_filter :remove_controller + # Returns the controller handling the current request. def self.current Thread.current[:application_controller] @@ -26,7 +29,7 @@ class ApplicationController < ActionController::Base redirect_to login_url, :alert => 'Access denied!' end - private + private def authenticate(role = 'any') # Attempt to retrieve authenticated user from controller instance or session... @@ -141,9 +144,5 @@ class ApplicationController < ActionController::Base def default_url_options(options = {}) {foodcoop: FoodsoftConfig.scope} end - - # Used to prevent accidently switching to :en in production mode. - def select_language - I18n.locale = :de - end + end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 55afb7b2..99f108ce 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -16,6 +16,7 @@ class HomeController < ApplicationController def update_profile if @current_user.update_attributes(params[:user]) + session[:locale] = @current_user.locale redirect_to my_profile_url, notice: I18n.t('home.changes_saved') else render :profile diff --git a/app/controllers/login_controller.rb b/app/controllers/login_controller.rb index 745d5e58..3aab74c5 100644 --- a/app/controllers/login_controller.rb +++ b/app/controllers/login_controller.rb @@ -58,6 +58,7 @@ class LoginController < ApplicationController if @user.save Membership.new(:user => @user, :group => @invite.group).save! @invite.destroy + session[:locale] = @user.locale redirect_to login_url, notice: I18n.t('login.controller.accept_invitation.notice') end end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index c89b818d..7de1b9de 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -11,6 +11,8 @@ class SessionsController < ApplicationController if user session[:user_id] = user.id session[:scope] = FoodsoftConfig.scope # Save scope in session to not allow switching between foodcoops with one account + session[:locale] = user.locale + if session[:return_to].present? redirect_to_url = session[:return_to] session[:return_to] = nil diff --git a/app/models/user.rb b/app/models/user.rb index 359bf51c..ab11da32 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,6 +3,7 @@ require 'digest/sha1' # specific user rights through memberships (see Group) class User < ActiveRecord::Base + include RailsSettings::Extend #TODO: acts_as_paraniod ?? has_many :memberships, :dependent => :destroy @@ -19,8 +20,11 @@ class User < ActiveRecord::Base has_many :pages, :foreign_key => 'updated_by' has_many :created_orders, :class_name => 'Order', :foreign_key => 'created_by_user_id', :dependent => :nullify - attr_accessor :password, :setting_attributes - + attr_accessor :password, :settings_attributes + + # makes the current_user (logged-in-user) available in models + cattr_accessor :current_user + validates_presence_of :nick, :email validates_presence_of :password, :on => :create validates_length_of :nick, :in => 2..25 @@ -32,53 +36,37 @@ class User < ActiveRecord::Base validates_length_of :password, :in => 5..25, :allow_blank => true before_validation :set_password - after_save :update_settings - - # Adds support for configuration settings (through "settings" attribute). - acts_as_configurable - - # makes the current_user (logged-in-user) available in models - cattr_accessor :current_user - - # User settings keys - # returns the User-settings and the translated description - def self.setting_keys - { - "notify.orderFinished" => I18n.t('model.user.notify.order_finished'), - "notify.negativeBalance" => I18n.t('model.user.notify.negative_balance'), - "notify.upcoming_tasks" => I18n.t('model.user.notify.upcoming_tasks'), - "messages.sendAsEmail" => I18n.t('model.user.notify.send_as_email'), - "profile.phoneIsPublic" => I18n.t('model.user.notify.phone_is_public'), - "profile.emailIsPublic" => I18n.t('model.user.notify.email_is_public'), - "profile.nameIsPublic" => I18n.t('model.user.notify.name_is_public') - } + after_initialize do + settings.defaults['profile'] = { 'language' => I18n.default_locale } unless settings.profile + settings.defaults['messages'] = { 'send_as_email' => true } unless settings.messages + settings.defaults['notify'] = { 'upcoming_tasks' => true } unless settings.notify end - # retuns the default setting for a NEW user - # for old records nil will returned - # TODO: integrate default behaviour in acts_as_configurable plugin - def settings_default(setting) - # define a default for the settings - defaults = { - "messages.sendAsEmail" => true, - "notify.upcoming_tasks" => true - } - return true if self.new_record? && defaults[setting] - end - - def update_settings - unless setting_attributes.nil? - for setting in User::setting_keys.keys - self.settings[setting] = setting_attributes[setting] && setting_attributes[setting] == '1' ? '1' : nil + + after_save do + return if settings_attributes.nil? + settings_attributes.each do |key, value| + value.each do |k, v| + case v + when '1' + value[k] = true + when '0' + value[k] = false + end end + self.settings.merge!(key, value) end end + def locale + settings.profile['language'] + end + def name [first_name, last_name].join(" ") end def receive_email? - settings['messages.sendAsEmail'] == "1" && email.present? + settings.messages['send_as_email'] == "1" && email.present? end # Sets the user's password. It will be stored encrypted along with a random salt. diff --git a/app/views/admin/users/show.html.haml b/app/views/admin/users/show.html.haml index a954b7d5..4d0cd1ab 100644 --- a/app/views/admin/users/show.html.haml +++ b/app/views/admin/users/show.html.haml @@ -20,10 +20,10 @@ .well %h4= t '.preference' %table.table - - for setting in User::setting_keys.keys + - @user.settings.profile.each do |key, setting| %tr - %td= User::setting_keys[setting] - %td= @user.settings[setting] == '1' ? t('simple_form.yes') : t('simple_form.no') + %td= t("simple_form.labels.settings.profile.#{key}") + %td= (setting != true and setting != false) ? setting : (setting === true ? t('simple_form.yes') : t('simple_form.no')) .span3 .well %h4= t '.groupabos' diff --git a/app/views/foodcoop/users/_users.html.haml b/app/views/foodcoop/users/_users.html.haml index 487f15e4..90b1de3a 100644 --- a/app/views/foodcoop/users/_users.html.haml +++ b/app/views/foodcoop/users/_users.html.haml @@ -14,9 +14,9 @@ - for user in @users %tr %td= user.nick - %td= user.name if @current_user.role_admin? || user.settings["profile.nameIsPublic"] == '1' - %td= user.email if @current_user.role_admin? || user.settings["profile.emailIsPublic"] == '1' - %td= user.phone if @current_user.role_admin? || user.settings["profile.phoneIsPublic"] == '1' + %td= user.name if @current_user.role_admin? || user.settings.profile["name_is_public"] + %td= user.email if @current_user.role_admin? || user.settings.profile["email_is_public"] + %td= user.phone if @current_user.role_admin? || user.settings.profile["phone_is_public"] %td= user.ordergroup_name %td= user.workgroups.collect(&:name).join(', ') %td= link_to_new_message(message_params: {mail_to: user.id}) diff --git a/app/views/shared/_user_form_fields.html.haml b/app/views/shared/_user_form_fields.html.haml index fa11b393..bfd27f49 100644 --- a/app/views/shared/_user_form_fields.html.haml +++ b/app/views/shared/_user_form_fields.html.haml @@ -5,11 +5,31 @@ = f.input :phone = f.input :password, :required => f.object.new_record? = f.input :password_confirmation -.control-group - .controls - - for setting in User::setting_keys.keys - %label.checkbox{:for => "user[setting_attributes][#{setting}]"} - = hidden_field_tag "user[setting_attributes][#{setting}]", '0' - = check_box_tag "user[setting_attributes][#{setting}]", '1', - f.object.settings[setting] == '1' || f.object.settings_default(setting) - = User::setting_keys[setting] + += f.simple_fields_for :settings_attributes do |s| + = s.simple_fields_for :profile, defaults: { inline_label: true } do |profile| + = profile.input 'language', as: :select, collection: available_locales, required: false, selected: f.object.settings.profile['language'] + + .settings + .settings-group + = s.simple_fields_for :profile, defaults: { inline_label: true } do |profile| + + %div{class: 'control-group h_wrapper'} + %h5{class: 'controls'} + = t 'simple_form.labels.settings.settings_group.privacy' + = profile.input 'phone_is_public', as: :boolean, label: false, input_html: { checked: f.object.settings.profile['phone_is_public'] } + = profile.input 'email_is_public', as: :boolean, label: false, input_html: { checked: f.object.settings.profile['email_is_public'] } + = profile.input 'name_is_public', as: :boolean, label: false, input_html: { checked: f.object.settings.profile['name_is_public'] } + + .settings-group + %div{class: 'control-group'} + %h5{class: 'controls'} + = t 'simple_form.labels.settings.settings_group.messages' + + = s.simple_fields_for :messages, defaults: { inline_label: true, label: false } do |messages| + = messages.input 'send_as_email', as: :boolean, input_html: { checked: f.object.settings.messages['send_as_email'] } + = s.simple_fields_for :notify, defaults: { inline_label: true, label: false } do |notify| + = notify.input 'order_finished', as: :boolean, input_html: { checked: f.object.settings.notify['order_finished'] } + = notify.input 'negative_balance', as: :boolean, input_html: { checked: f.object.settings.notify['negative_balance'] } + = notify.input 'upcoming_tasks', as: :boolean, input_html: { checked: f.object.settings.notify['upcoming_tasks'] } + diff --git a/app/workers/user_notifier.rb b/app/workers/user_notifier.rb index 7be2d17c..55041465 100644 --- a/app/workers/user_notifier.rb +++ b/app/workers/user_notifier.rb @@ -18,7 +18,7 @@ class UserNotifier Order.find(order_id).group_orders.each do |group_order| group_order.ordergroup.users.each do |user| begin - Mailer.order_result(user, group_order).deliver if user.settings["notify.orderFinished"] == '1' + Mailer.order_result(user, group_order).deliver if user.settings.notify["order_finished"] rescue Rails.logger.warn "Can't deliver mail to #{user.email}" end @@ -34,7 +34,7 @@ class UserNotifier Ordergroup.find(ordergroup_id).users.each do |user| begin - Mailer.negative_balance(user, transaction).deliver if user.settings["notify.negativeBalance"] == '1' + Mailer.negative_balance(user, transaction).deliver if user.settings.notify["negative_balance"] rescue Rails.logger.warn "Can't deliver mail to #{user.email}" end diff --git a/config/application.rb b/config/application.rb index 32ea295b..6d519277 100644 --- a/config/application.rb +++ b/config/application.rb @@ -31,7 +31,7 @@ module Foodsoft # Internationalization. config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')] - config.i18n.default_locale = :de + config.i18n.default_locale = :en # Configure the default encoding used in templates for Ruby 1.9. config.encoding = "utf-8" diff --git a/config/locales/de.yml b/config/locales/de.yml index 200cd086..11abe693 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -465,11 +465,11 @@ de: title_articles: Artikel unit: Einheit stock_article_for_adding: - action_add_to_delivery: 'Liefern' - action_edit: 'Bearbeiten' - action_other_price: 'Kopieren' + action_add_to_delivery: Liefern + action_edit: Bearbeiten + action_other_price: Kopieren stock_article_form: - copy_stock_article: 'Lagerartikel kopieren' + copy_stock_article: Lagerartikel kopieren stock_change_fields: remove_article: Artikel aus Lieferung entfernen suppliers_overview: Lieferantenübersicht @@ -1179,14 +1179,6 @@ de: redirect: Weiterleiting auf [[%{title}]]... user: no_ordergroup: keine Bestellgruppe - notify: - email_is_public: E-Mail ist für Mitglieder sichtbar - name_is_public: Name ist für Mitglieder sichtbar - negative_balance: Informiere mich, falls meine Bestellgruppe ins Minus rutscht. - order_finished: Informier mich über meine Bestellergebnisse (nach Ende der Bestellung). - phone_is_public: Telefon ist für Mitglieder sichtbar - send_as_email: Bekomme Nachrichten als Emails. - upcoming_tasks: Erinnere mich an anstehende Aufgaben. navigation: admin: home: Übersicht @@ -1534,11 +1526,11 @@ de: units_to_order: Anzahl gelieferter Gebinde update_current_price: Ändert auch den Preis für aktuelle Bestellungen stock_article: - supplier: copy_stock_article: name: Bitte ändern edit_stock_article: - price: ! '