bootstrap-on 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +90 -0
- data/LICENSE.txt +20 -0
- data/README.md +98 -0
- data/Rakefile +38 -0
- data/VERSION +1 -0
- data/bootstrap-on.gemspec +127 -0
- data/lib/bootstrap-on.rb +6 -0
- data/lib/bootstrap-on/bs_admin.rb +138 -0
- data/lib/bootstrap-on/bs_admin_page.rb +55 -0
- data/lib/bootstrap-on/templates/.DS_Store +0 -0
- data/lib/bootstrap-on/templates/account/.DS_Store +0 -0
- data/lib/bootstrap-on/templates/account/activerecord.rb.tt +38 -0
- data/lib/bootstrap-on/templates/account/couchrest.rb.tt +66 -0
- data/lib/bootstrap-on/templates/account/datamapper.rb.tt +55 -0
- data/lib/bootstrap-on/templates/account/mini_record.rb.tt +41 -0
- data/lib/bootstrap-on/templates/account/mongoid.rb.tt +53 -0
- data/lib/bootstrap-on/templates/account/mongomapper.rb.tt +46 -0
- data/lib/bootstrap-on/templates/account/seeds.rb.tt +28 -0
- data/lib/bootstrap-on/templates/account/sequel.rb.tt +52 -0
- data/lib/bootstrap-on/templates/app.rb.tt +36 -0
- data/lib/bootstrap-on/templates/app/controllers/base.rb +6 -0
- data/lib/bootstrap-on/templates/app/controllers/sessions.rb.tt +26 -0
- data/lib/bootstrap-on/templates/app/helpers/application_helper.rb +23 -0
- data/lib/bootstrap-on/templates/assets/.DS_Store +0 -0
- data/lib/bootstrap-on/templates/assets/css/bootstrap-responsive.css +815 -0
- data/lib/bootstrap-on/templates/assets/css/bootstrap-responsive.min.css +9 -0
- data/lib/bootstrap-on/templates/assets/css/bootstrap.css +4983 -0
- data/lib/bootstrap-on/templates/assets/css/bootstrap.min.css +9 -0
- data/lib/bootstrap-on/templates/assets/ico/apple-touch-icon-114-precomposed.png +0 -0
- data/lib/bootstrap-on/templates/assets/ico/apple-touch-icon-144-precomposed.png +0 -0
- data/lib/bootstrap-on/templates/assets/ico/apple-touch-icon-57-precomposed.png +0 -0
- data/lib/bootstrap-on/templates/assets/ico/apple-touch-icon-72-precomposed.png +0 -0
- data/lib/bootstrap-on/templates/assets/ico/favicon.ico +0 -0
- data/lib/bootstrap-on/templates/assets/img/glyphicons-halflings-white.png +0 -0
- data/lib/bootstrap-on/templates/assets/img/glyphicons-halflings.png +0 -0
- data/lib/bootstrap-on/templates/assets/js/.DS_Store +0 -0
- data/lib/bootstrap-on/templates/assets/js/bootstrap.js +1825 -0
- data/lib/bootstrap-on/templates/assets/js/bootstrap.min.js +6 -0
- data/lib/bootstrap-on/templates/assets/js/jquery-min.js +4 -0
- data/lib/bootstrap-on/templates/assets/js/jquery-ujs.js +92 -0
- data/lib/bootstrap-on/templates/assets/js/jquery.js +9404 -0
- data/lib/bootstrap-on/templates/erb/app/base/index.erb.tt +16 -0
- data/lib/bootstrap-on/templates/erb/app/layouts/application.erb.tt +79 -0
- data/lib/bootstrap-on/templates/erb/app/sessions/new.erb.tt +98 -0
- data/lib/bootstrap-on/templates/erb/page/_form.erb.tt +16 -0
- data/lib/bootstrap-on/templates/erb/page/edit.erb.tt +26 -0
- data/lib/bootstrap-on/templates/erb/page/index.erb.tt +41 -0
- data/lib/bootstrap-on/templates/erb/page/new.erb.tt +23 -0
- data/lib/bootstrap-on/templates/haml/app/base/index.haml.tt +23 -0
- data/lib/bootstrap-on/templates/haml/app/layouts/application.haml.tt +31 -0
- data/lib/bootstrap-on/templates/haml/app/sessions/new.haml.tt +30 -0
- data/lib/bootstrap-on/templates/haml/page/_form.haml.tt +12 -0
- data/lib/bootstrap-on/templates/haml/page/edit.haml.tt +15 -0
- data/lib/bootstrap-on/templates/haml/page/index.haml.tt +29 -0
- data/lib/bootstrap-on/templates/haml/page/new.haml.tt +14 -0
- data/lib/bootstrap-on/templates/page/controller.rb.tt +47 -0
- data/lib/bootstrap-on/templates/slim/app/base/index.slim.tt +23 -0
- data/lib/bootstrap-on/templates/slim/app/layouts/application.slim.tt +31 -0
- data/lib/bootstrap-on/templates/slim/app/sessions/new.slim.tt +31 -0
- data/lib/bootstrap-on/templates/slim/page/_form.slim.tt +14 -0
- data/lib/bootstrap-on/templates/slim/page/edit.slim.tt +16 -0
- data/lib/bootstrap-on/templates/slim/page/index.slim.tt +30 -0
- data/lib/bootstrap-on/templates/slim/page/new.slim.tt +15 -0
- data/test/bootstrap-on/test_bs_admin_generator.rb +332 -0
- data/test/bootstrap-on/test_bs_admin_page_generator.rb +132 -0
- data/test/helper.rb +98 -0
- data/test/load_paths.rb +6 -0
- data/test/test_bootstrap-on.rb +15 -0
- metadata +240 -0
@@ -0,0 +1,55 @@
|
|
1
|
+
module Padrino
|
2
|
+
module Generators
|
3
|
+
##
|
4
|
+
# Defines the generator for creating a new admin page.
|
5
|
+
#
|
6
|
+
class BsAdminPage < Thor::Group
|
7
|
+
attr_accessor :default_orm
|
8
|
+
|
9
|
+
# Add this generator to our padrino-gen
|
10
|
+
Padrino::Generators.add_generator(:bs_admin_page, self)
|
11
|
+
|
12
|
+
# Define the source template root
|
13
|
+
def self.source_root; File.expand_path(File.dirname(__FILE__)); end
|
14
|
+
# Defines the "banner" text for the CLI.
|
15
|
+
def self.banner; "padrino g bs_admin_page [model]"; end
|
16
|
+
|
17
|
+
# Include related modules
|
18
|
+
include Thor::Actions
|
19
|
+
include Padrino::Generators::Actions
|
20
|
+
include Padrino::Generators::Admin::Actions
|
21
|
+
|
22
|
+
desc "Description:\n\n\tpadrino g bs_admin_page [model(s)] - Generates a new Padrino Admin page with Twitter Bootstrapped integrated"
|
23
|
+
|
24
|
+
argument :models, :desc => "The name(s) of your model(s)", :type => :array
|
25
|
+
class_option :skip_migration, :aliases => "-s", :default => false, :type => :boolean
|
26
|
+
class_option :root, :desc => "The root destination", :aliases => '-r', :type => :string
|
27
|
+
class_option :destroy, :aliases => '-d', :default => false, :type => :boolean
|
28
|
+
|
29
|
+
# Show help if no argv given
|
30
|
+
require_arguments!
|
31
|
+
|
32
|
+
# Create controller for admin
|
33
|
+
def create_controller
|
34
|
+
self.destination_root = options[:root]
|
35
|
+
if in_app_root?
|
36
|
+
models.each do |model|
|
37
|
+
@orm = default_orm || Padrino::Admin::Generators::Orm.new(model, adapter)
|
38
|
+
self.behavior = :revoke if options[:destroy]
|
39
|
+
empty_directory destination_root("/admin/views/#{@orm.name_plural}")
|
40
|
+
|
41
|
+
template "templates/page/controller.rb.tt", destination_root("/admin/controllers/#{@orm.name_plural}.rb")
|
42
|
+
template "templates/#{ext}/page/_form.#{ext}.tt", destination_root("/admin/views/#{@orm.name_plural}/_form.#{ext}")
|
43
|
+
template "templates/#{ext}/page/edit.#{ext}.tt", destination_root("/admin/views/#{@orm.name_plural}/edit.#{ext}")
|
44
|
+
template "templates/#{ext}/page/index.#{ext}.tt", destination_root("/admin/views/#{@orm.name_plural}/index.#{ext}")
|
45
|
+
template "templates/#{ext}/page/new.#{ext}.tt", destination_root("/admin/views/#{@orm.name_plural}/new.#{ext}")
|
46
|
+
|
47
|
+
options[:destroy] ? remove_project_module(@orm.name_plural) : add_project_module(@orm.name_plural)
|
48
|
+
end
|
49
|
+
else
|
50
|
+
say "You are not at the root of a Padrino application! (config/boot.rb not found)"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end # AdminPage
|
54
|
+
end # Generators
|
55
|
+
end # Padrino
|
Binary file
|
Binary file
|
@@ -0,0 +1,38 @@
|
|
1
|
+
class <%= @model_name %> < ActiveRecord::Base
|
2
|
+
attr_accessor :password, :password_confirmation
|
3
|
+
|
4
|
+
# Validations
|
5
|
+
validates_presence_of :email, :role
|
6
|
+
validates_presence_of :password, :if => :password_required
|
7
|
+
validates_presence_of :password_confirmation, :if => :password_required
|
8
|
+
validates_length_of :password, :within => 4..40, :if => :password_required
|
9
|
+
validates_confirmation_of :password, :if => :password_required
|
10
|
+
validates_length_of :email, :within => 3..100
|
11
|
+
validates_uniqueness_of :email, :case_sensitive => false
|
12
|
+
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
|
13
|
+
validates_format_of :role, :with => /[A-Za-z]/
|
14
|
+
|
15
|
+
# Callbacks
|
16
|
+
before_save :encrypt_password, :if => :password_required
|
17
|
+
|
18
|
+
##
|
19
|
+
# This method is for authentication purpose
|
20
|
+
#
|
21
|
+
def self.authenticate(email, password)
|
22
|
+
account = first(:conditions => { :email => email }) if email.present?
|
23
|
+
account && account.has_password?(password) ? account : nil
|
24
|
+
end
|
25
|
+
|
26
|
+
def has_password?(password)
|
27
|
+
::BCrypt::Password.new(crypted_password) == password
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
def encrypt_password
|
32
|
+
self.crypted_password = ::BCrypt::Password.create(password)
|
33
|
+
end
|
34
|
+
|
35
|
+
def password_required
|
36
|
+
crypted_password.blank? || password.present?
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
class <%= @model_name %> < CouchRest::Model::Base
|
2
|
+
attr_accessor :password, :password_confirmation
|
3
|
+
|
4
|
+
# Properties
|
5
|
+
property :name
|
6
|
+
property :surname
|
7
|
+
property :email
|
8
|
+
property :crypted_password
|
9
|
+
property :role
|
10
|
+
|
11
|
+
view_by :email
|
12
|
+
|
13
|
+
# Validations
|
14
|
+
validates_presence_of :email, :role
|
15
|
+
validates_presence_of :password, :if => :password_required
|
16
|
+
validates_presence_of :password_confirmation, :if => :password_required
|
17
|
+
validates_length_of :password, :within => 4..40, :if => :password_required
|
18
|
+
validates_confirmation_of :password, :if => :password_required
|
19
|
+
validates_length_of :email, :within => 3..100
|
20
|
+
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
|
21
|
+
validates_format_of :role, :with => /[A-Za-z]/
|
22
|
+
validate :unique_email_validator
|
23
|
+
|
24
|
+
# Callbacks
|
25
|
+
before_save :encrypt_password, :if => :password_required
|
26
|
+
|
27
|
+
##
|
28
|
+
# This method is for authentication purpose
|
29
|
+
#
|
30
|
+
def self.authenticate(email, password)
|
31
|
+
account = find_by_email(email)
|
32
|
+
account if account && account.has_password?(password)
|
33
|
+
end
|
34
|
+
|
35
|
+
def has_password?(password)
|
36
|
+
::BCrypt::Password.new(crypted_password) == password
|
37
|
+
end
|
38
|
+
|
39
|
+
##
|
40
|
+
# This method is used by AuthenticationHelper
|
41
|
+
#
|
42
|
+
def self.find_by_id(id)
|
43
|
+
get(id)
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
def encrypt_password
|
48
|
+
self.crypted_password = ::BCrypt::Password.create(password)
|
49
|
+
end
|
50
|
+
|
51
|
+
def password_required
|
52
|
+
crypted_password.blank? || password.present?
|
53
|
+
end
|
54
|
+
|
55
|
+
def unique_email_validator
|
56
|
+
account = self.class.find_by_email(email)
|
57
|
+
|
58
|
+
# didn't find email in the database
|
59
|
+
return if account.nil?
|
60
|
+
|
61
|
+
# account with same email in database is this account
|
62
|
+
return if has_key?('_id') && self['_id'] == account['_id']
|
63
|
+
|
64
|
+
errors.add(:email, "is not unique")
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
class <%= @model_name %>
|
2
|
+
include DataMapper::Resource
|
3
|
+
include DataMapper::Validate
|
4
|
+
attr_accessor :password, :password_confirmation
|
5
|
+
|
6
|
+
# Properties
|
7
|
+
property :id, Serial
|
8
|
+
property :name, String
|
9
|
+
property :surname, String
|
10
|
+
property :email, String
|
11
|
+
property :crypted_password, String, :length => 70
|
12
|
+
property :role, String
|
13
|
+
|
14
|
+
# Validations
|
15
|
+
validates_presence_of :email, :role
|
16
|
+
validates_presence_of :password, :if => :password_required
|
17
|
+
validates_presence_of :password_confirmation, :if => :password_required
|
18
|
+
validates_length_of :password, :min => 4, :max => 40, :if => :password_required
|
19
|
+
validates_confirmation_of :password, :if => :password_required
|
20
|
+
validates_length_of :email, :min => 3, :max => 100
|
21
|
+
validates_uniqueness_of :email, :case_sensitive => false
|
22
|
+
validates_format_of :email, :with => :email_address
|
23
|
+
validates_format_of :role, :with => /[A-Za-z]/
|
24
|
+
|
25
|
+
# Callbacks
|
26
|
+
before :save, :encrypt_password
|
27
|
+
|
28
|
+
##
|
29
|
+
# This method is for authentication purpose
|
30
|
+
#
|
31
|
+
def self.authenticate(email, password)
|
32
|
+
account = first(:conditions => { :email => email }) if email.present?
|
33
|
+
account && account.has_password?(password) ? account : nil
|
34
|
+
end
|
35
|
+
|
36
|
+
##
|
37
|
+
# This method is used by AuthenticationHelper
|
38
|
+
#
|
39
|
+
def self.find_by_id(id)
|
40
|
+
get(id) rescue nil
|
41
|
+
end
|
42
|
+
|
43
|
+
def has_password?(password)
|
44
|
+
::BCrypt::Password.new(crypted_password) == password
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
def password_required
|
49
|
+
crypted_password.blank? || password.present?
|
50
|
+
end
|
51
|
+
|
52
|
+
def encrypt_password
|
53
|
+
self.crypted_password = ::BCrypt::Password.create(password) if password.present?
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
class <%= @model_name %> < ActiveRecord::Base
|
2
|
+
attr_accessor :password, :password_confirmation
|
3
|
+
|
4
|
+
# Fields
|
5
|
+
field :name, :surname, :email, :crypted_password, :role
|
6
|
+
|
7
|
+
# Validations
|
8
|
+
validates_presence_of :email, :role
|
9
|
+
validates_presence_of :password, :if => :password_required
|
10
|
+
validates_presence_of :password_confirmation, :if => :password_required
|
11
|
+
validates_length_of :password, :within => 4..40, :if => :password_required
|
12
|
+
validates_confirmation_of :password, :if => :password_required
|
13
|
+
validates_length_of :email, :within => 3..100
|
14
|
+
validates_uniqueness_of :email, :case_sensitive => false
|
15
|
+
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
|
16
|
+
validates_format_of :role, :with => /[A-Za-z]/
|
17
|
+
|
18
|
+
# Callbacks
|
19
|
+
before_save :encrypt_password, :if => :password_required
|
20
|
+
|
21
|
+
##
|
22
|
+
# This method is for authentication purpose
|
23
|
+
#
|
24
|
+
def self.authenticate(email, password)
|
25
|
+
account = first(:conditions => { :email => email }) if email.present?
|
26
|
+
account && account.has_password?(password) ? account : nil
|
27
|
+
end
|
28
|
+
|
29
|
+
def has_password?(password)
|
30
|
+
::BCrypt::Password.new(crypted_password) == password
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
def encrypt_password
|
35
|
+
self.crypted_password = ::BCrypt::Password.create(password)
|
36
|
+
end
|
37
|
+
|
38
|
+
def password_required
|
39
|
+
crypted_password.blank? || password.present?
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
class <%= @model_name %>
|
2
|
+
include Mongoid::Document
|
3
|
+
attr_accessor :password, :password_confirmation
|
4
|
+
|
5
|
+
# Fields
|
6
|
+
field :name, :type => String
|
7
|
+
field :surname, :type => String
|
8
|
+
field :email, :type => String
|
9
|
+
field :crypted_password, :type => String
|
10
|
+
field :role, :type => String
|
11
|
+
|
12
|
+
# Validations
|
13
|
+
validates_presence_of :email, :role
|
14
|
+
validates_presence_of :password, :if => :password_required
|
15
|
+
validates_presence_of :password_confirmation, :if => :password_required
|
16
|
+
validates_length_of :password, :within => 4..40, :if => :password_required
|
17
|
+
validates_confirmation_of :password, :if => :password_required
|
18
|
+
validates_length_of :email, :within => 3..100
|
19
|
+
validates_uniqueness_of :email, :case_sensitive => false
|
20
|
+
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
|
21
|
+
validates_format_of :role, :with => /[A-Za-z]/
|
22
|
+
|
23
|
+
# Callbacks
|
24
|
+
before_save :encrypt_password, :if => :password_required
|
25
|
+
|
26
|
+
##
|
27
|
+
# This method is for authentication purpose
|
28
|
+
#
|
29
|
+
def self.authenticate(email, password)
|
30
|
+
account = first(:conditions => { :email => email }) if email.present?
|
31
|
+
account && account.has_password?(password) ? account : nil
|
32
|
+
end
|
33
|
+
|
34
|
+
##
|
35
|
+
# This method is used by AuthenticationHelper
|
36
|
+
#
|
37
|
+
def self.find_by_id(id)
|
38
|
+
find(id) rescue nil
|
39
|
+
end
|
40
|
+
|
41
|
+
def has_password?(password)
|
42
|
+
::BCrypt::Password.new(crypted_password) == password
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
def encrypt_password
|
47
|
+
self.crypted_password = ::BCrypt::Password.create(self.password)
|
48
|
+
end
|
49
|
+
|
50
|
+
def password_required
|
51
|
+
crypted_password.blank? || self.password.present?
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
class <%= @model_name %>
|
2
|
+
include MongoMapper::Document
|
3
|
+
attr_accessor :password, :password_confirmation
|
4
|
+
|
5
|
+
# Keys
|
6
|
+
key :name, String
|
7
|
+
key :surname, String
|
8
|
+
key :email, String
|
9
|
+
key :crypted_password, String
|
10
|
+
key :role, String
|
11
|
+
|
12
|
+
# Validations
|
13
|
+
validates_presence_of :email, :role
|
14
|
+
validates_presence_of :password, :if => :password_required
|
15
|
+
validates_presence_of :password_confirmation, :if => :password_required
|
16
|
+
validates_length_of :password, :within => 4..40, :if => :password_required
|
17
|
+
validates_confirmation_of :password, :if => :password_required
|
18
|
+
validates_length_of :email, :within => 3..100
|
19
|
+
validates_uniqueness_of :email, :case_sensitive => false
|
20
|
+
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
|
21
|
+
validates_format_of :role, :with => /[A-Za-z]/
|
22
|
+
|
23
|
+
# Callbacks
|
24
|
+
before_save :encrypt_password, :if => :password_required
|
25
|
+
|
26
|
+
##
|
27
|
+
# This method is for authentication purpose
|
28
|
+
#
|
29
|
+
def self.authenticate(email, password)
|
30
|
+
account = first(:email => email) if email.present?
|
31
|
+
account && account.has_password?(password) ? account : nil
|
32
|
+
end
|
33
|
+
|
34
|
+
def has_password?(password)
|
35
|
+
::BCrypt::Password.new(crypted_password) == password
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
def encrypt_password
|
40
|
+
self.crypted_password = ::BCrypt::Password.create(password)
|
41
|
+
end
|
42
|
+
|
43
|
+
def password_required
|
44
|
+
crypted_password.blank? || password.present?
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# Seed add you the ability to populate your db.
|
2
|
+
# We provide you a basic shell for interaction with the end user.
|
3
|
+
# So try some code like below:
|
4
|
+
#
|
5
|
+
# name = shell.ask("What's your name?")
|
6
|
+
# shell.say name
|
7
|
+
#
|
8
|
+
email = shell.ask "Which email do you want use for logging into admin?"
|
9
|
+
password = shell.ask "Tell me the password to use:"
|
10
|
+
|
11
|
+
shell.say ""
|
12
|
+
|
13
|
+
account = <%= @model_name %>.create(:email => email, :name => "Foo", :surname => "Bar", :password => password, :password_confirmation => password, :role => "admin")
|
14
|
+
|
15
|
+
if account.valid?
|
16
|
+
shell.say "================================================================="
|
17
|
+
shell.say "<%= @model_name %> has been successfully created, now you can login with:"
|
18
|
+
shell.say "================================================================="
|
19
|
+
shell.say " email: #{email}"
|
20
|
+
shell.say " password: #{password}"
|
21
|
+
shell.say "================================================================="
|
22
|
+
else
|
23
|
+
shell.say "Sorry but some thing went wrong!"
|
24
|
+
shell.say ""
|
25
|
+
account.errors.full_messages.each { |m| shell.say " - #{m}" }
|
26
|
+
end
|
27
|
+
|
28
|
+
shell.say ""
|
@@ -0,0 +1,52 @@
|
|
1
|
+
class <%= @model_name %> < ::Sequel::Model
|
2
|
+
|
3
|
+
plugin :validation_helpers
|
4
|
+
|
5
|
+
attr_accessor :password, :password_confirmation
|
6
|
+
|
7
|
+
def validate
|
8
|
+
validates_presence :email
|
9
|
+
validates_presence :role
|
10
|
+
validates_presence :password if password_required
|
11
|
+
validates_presence :password_confirmation if password_required
|
12
|
+
validates_length_range 4..40, :password unless password.blank?
|
13
|
+
errors.add(:password_confirmation, 'must confirm password') if !password.blank? && password != password_confirmation
|
14
|
+
validates_length_range 3..100, :email unless email.blank?
|
15
|
+
validates_unique :email unless email.blank?
|
16
|
+
validates_format /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :email unless email.blank?
|
17
|
+
validates_format /[A-Za-z]/, :role unless role.blank?
|
18
|
+
end
|
19
|
+
|
20
|
+
# Callbacks
|
21
|
+
def before_save
|
22
|
+
encrypt_password
|
23
|
+
end
|
24
|
+
|
25
|
+
##
|
26
|
+
# This method is for authentication purpose
|
27
|
+
#
|
28
|
+
def self.authenticate(email, password)
|
29
|
+
account = filter(:email => email).first
|
30
|
+
account && account.has_password?(password) ? account : nil
|
31
|
+
end
|
32
|
+
|
33
|
+
##
|
34
|
+
# Replace AR method
|
35
|
+
#
|
36
|
+
def self.find_by_id(id)
|
37
|
+
self[id] rescue nil
|
38
|
+
end
|
39
|
+
|
40
|
+
def has_password?(password)
|
41
|
+
::BCrypt::Password.new(self.crypted_password) == password
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
def encrypt_password
|
46
|
+
self.crypted_password = ::BCrypt::Password.create(password) if password.present?
|
47
|
+
end
|
48
|
+
|
49
|
+
def password_required
|
50
|
+
self.crypted_password.blank? || password.present?
|
51
|
+
end
|
52
|
+
end
|