Skip to content

Commit

Permalink
added concerns and fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
mokevnin committed Aug 9, 2013
1 parent 3031c82 commit 01edad6
Show file tree
Hide file tree
Showing 18 changed files with 65 additions and 57 deletions.
1 change: 0 additions & 1 deletion app/assets/javascripts/application.js
Expand Up @@ -13,7 +13,6 @@
//= require jquery
//= require jquery_ujs
//= require twitter/bootstrap
//= require turbolinks
//= require chosen-jquery
//= require_self

Expand Down
4 changes: 2 additions & 2 deletions 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?

Expand Down
25 changes: 25 additions & 0 deletions 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
8 changes: 8 additions & 0 deletions 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
17 changes: 17 additions & 0 deletions 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
2 changes: 1 addition & 1 deletion 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)
Expand Down
3 changes: 2 additions & 1 deletion app/models/user.rb
@@ -1,12 +1,13 @@
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
has_many :topic_comments, dependent: :restrict_with_exception

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
Expand Down
2 changes: 1 addition & 1 deletion app/types/application_type.rb
@@ -1,4 +1,4 @@
module BaseType
module ApplicationType
extend ActiveSupport::Concern

module ClassMethods
Expand Down
2 changes: 1 addition & 1 deletion app/types/application_type_without_active_record.rb
@@ -1,4 +1,4 @@
module BaseTypeWithoutActiveRecord
module ApplicationTypeWithoutActiveRecord
extend ActiveSupport::Concern

included do
Expand Down
2 changes: 1 addition & 1 deletion app/types/password_confirmation_type.rb
@@ -1,5 +1,5 @@
class PasswordConfirmationType
include BaseTypeWithoutActiveRecord
include ApplicationTypeWithoutActiveRecord

attribute :email, String

Expand Down
2 changes: 1 addition & 1 deletion 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
2 changes: 1 addition & 1 deletion app/types/user_password_edit_type.rb
@@ -1,5 +1,5 @@
class UserPasswordEditType < User
include BaseType
include ApplicationType

permit :password, :password_confirmation

Expand Down
4 changes: 3 additions & 1 deletion 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
2 changes: 1 addition & 1 deletion app/types/user_sign_in_type.rb
@@ -1,5 +1,5 @@
class UserSignInType
include BaseTypeWithoutActiveRecord
include ApplicationTypeWithoutActiveRecord

attribute :email, String
attribute :password, String
Expand Down
23 changes: 0 additions & 23 deletions lib/auth_helper.rb

This file was deleted.

6 changes: 0 additions & 6 deletions lib/custom_url_helper.rb

This file was deleted.

15 changes: 0 additions & 15 deletions lib/flash_helper.rb

This file was deleted.

2 changes: 1 addition & 1 deletion test/test_helper.rb
Expand Up @@ -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.
#
Expand Down

0 comments on commit 01edad6

Please sign in to comment.