diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 36e8c17..f8fa84f 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -13,7 +13,6 @@ //= require jquery //= require jquery_ujs //= require twitter/bootstrap -//= require turbolinks //= require chosen-jquery //= require_self diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 33ac0ec..68b84a4 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,6 +1,6 @@ class ApplicationController < ActionController::Base - include AuthHelper - include FlashHelper + include Concerns::AuthManagment + include Concerns::FlashHelper helper_method :current_user, :signed_in? diff --git a/app/controllers/concerns/auth_managment.rb b/app/controllers/concerns/auth_managment.rb new file mode 100644 index 0000000..4b593d5 --- /dev/null +++ b/app/controllers/concerns/auth_managment.rb @@ -0,0 +1,25 @@ +module Concerns + module AuthManagment + def sign_in(user) + session[:user_id] = user.id + end + + def sign_out + session[:user_id] = nil + end + + def signed_in? + current_user + end + + def authenticate_user! + unless signed_in? + redirect_to new_session_path + end + end + + def current_user + @current_user ||= User.active.where(id: session[:user_id]).first + end + end +end diff --git a/app/controllers/concerns/custom_url_helper.rb b/app/controllers/concerns/custom_url_helper.rb new file mode 100644 index 0000000..db92f67 --- /dev/null +++ b/app/controllers/concerns/custom_url_helper.rb @@ -0,0 +1,8 @@ +module Concerns + module CustomUrlHelper + + def sign_in_via_social_network_cpath(provider) + "/auth/#{provider}" + end + end +end diff --git a/app/controllers/concerns/flash_helper.rb b/app/controllers/concerns/flash_helper.rb new file mode 100644 index 0000000..4edf6ae --- /dev/null +++ b/app/controllers/concerns/flash_helper.rb @@ -0,0 +1,17 @@ +module Concerns + module FlashHelper + def f(key, options = {}) + scope = [:flash] + scope << params[:controller].split('/') + scope << params[:action] + + msg = I18n.t(key, scope: scope) + Rails.logger.debug(Term::ANSIColor.green("flash: #{msg}")) + if options[:now] + flash.now[key] = msg + else + flash[key] = msg + end + end + end +end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index c8382d8..c281f5d 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,5 +1,5 @@ module ApplicationHelper - include CustomUrlHelper + include Concerns::CustomUrlHelper def ham(model, attribute) model.to_s.classify.constantize.human_attribute_name(attribute) diff --git a/app/models/user.rb b/app/models/user.rb index cc4c828..c4d8c2a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,5 +1,5 @@ class User < ActiveRecord::Base - has_secure_password + has_secure_password validations: false has_many :authorizations, dependent: :destroy has_many :topics, foreign_key: :creator_id, dependent: :restrict_with_exception @@ -7,6 +7,7 @@ class User < ActiveRecord::Base validates :email, uniqueness: true, presence: true, email: true validates :login, uniqueness: true, presence: true + validates :password_digest, presence: true state_machine initial: :waiting_confirmation do state :waiting_confirmation diff --git a/app/types/application_type.rb b/app/types/application_type.rb index cb76b3e..a9f16e5 100644 --- a/app/types/application_type.rb +++ b/app/types/application_type.rb @@ -1,4 +1,4 @@ -module BaseType +module ApplicationType extend ActiveSupport::Concern module ClassMethods diff --git a/app/types/application_type_without_active_record.rb b/app/types/application_type_without_active_record.rb index 99ac592..5fb3eb4 100644 --- a/app/types/application_type_without_active_record.rb +++ b/app/types/application_type_without_active_record.rb @@ -1,4 +1,4 @@ -module BaseTypeWithoutActiveRecord +module ApplicationTypeWithoutActiveRecord extend ActiveSupport::Concern included do diff --git a/app/types/password_confirmation_type.rb b/app/types/password_confirmation_type.rb index 62ba104..8b5df99 100644 --- a/app/types/password_confirmation_type.rb +++ b/app/types/password_confirmation_type.rb @@ -1,5 +1,5 @@ class PasswordConfirmationType - include BaseTypeWithoutActiveRecord + include ApplicationTypeWithoutActiveRecord attribute :email, String diff --git a/app/types/topic_type.rb b/app/types/topic_type.rb index 48d23e8..acdb0a0 100644 --- a/app/types/topic_type.rb +++ b/app/types/topic_type.rb @@ -1,5 +1,5 @@ class TopicType < Topic - include BaseType + include ApplicationType permit :name, :body, :state_event, category_hub_ids: [] end diff --git a/app/types/user_password_edit_type.rb b/app/types/user_password_edit_type.rb index f31602f..c964521 100644 --- a/app/types/user_password_edit_type.rb +++ b/app/types/user_password_edit_type.rb @@ -1,5 +1,5 @@ class UserPasswordEditType < User - include BaseType + include ApplicationType permit :password, :password_confirmation diff --git a/app/types/user_registration_type.rb b/app/types/user_registration_type.rb index 650eb3d..ed69a8d 100644 --- a/app/types/user_registration_type.rb +++ b/app/types/user_registration_type.rb @@ -1,5 +1,7 @@ class UserRegistrationType < User - include BaseType + include ApplicationType + + has_secure_password permit :login, :email, :password, :password_confirmation end diff --git a/app/types/user_sign_in_type.rb b/app/types/user_sign_in_type.rb index e5adb37..fb4a50f 100644 --- a/app/types/user_sign_in_type.rb +++ b/app/types/user_sign_in_type.rb @@ -1,5 +1,5 @@ class UserSignInType - include BaseTypeWithoutActiveRecord + include ApplicationTypeWithoutActiveRecord attribute :email, String attribute :password, String diff --git a/lib/auth_helper.rb b/lib/auth_helper.rb deleted file mode 100644 index ecb7fd8..0000000 --- a/lib/auth_helper.rb +++ /dev/null @@ -1,23 +0,0 @@ -module AuthHelper - def sign_in(user) - session[:user_id] = user.id - end - - def sign_out - session[:user_id] = nil - end - - def signed_in? - current_user - end - - def authenticate_user! - unless signed_in? - redirect_to new_session_path - end - end - - def current_user - @current_user ||= User.active.where(id: session[:user_id]).first - end -end diff --git a/lib/custom_url_helper.rb b/lib/custom_url_helper.rb deleted file mode 100644 index 9fbb70b..0000000 --- a/lib/custom_url_helper.rb +++ /dev/null @@ -1,6 +0,0 @@ -module CustomUrlHelper - - def sign_in_via_social_network_cpath(provider) - "/auth/#{provider}" - end -end diff --git a/lib/flash_helper.rb b/lib/flash_helper.rb deleted file mode 100644 index c3c2669..0000000 --- a/lib/flash_helper.rb +++ /dev/null @@ -1,15 +0,0 @@ -module FlashHelper - def f(key, options = {}) - scope = [:flash] - scope << params[:controller].split('/') - scope << params[:action] - - msg = I18n.t(key, scope: scope) - Rails.logger.debug(Term::ANSIColor.green("flash: #{msg}")) - if options[:now] - flash.now[key] = msg - else - flash[key] = msg - end - end -end diff --git a/test/test_helper.rb b/test/test_helper.rb index b4b8964..4e1539a 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -50,7 +50,7 @@ class ActiveSupport::TestCase ActiveRecord::Migration.check_pending! include FactoryGirl::Syntax::Methods - include AuthHelper + include Concerns::AuthManagment # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. #