padrino-admin 0.16.0.pre3 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Rakefile +5 -5
- data/lib/padrino-admin/access_control.rb +10 -9
- data/lib/padrino-admin/generators/actions.rb +8 -8
- data/lib/padrino-admin/generators/admin_app.rb +62 -62
- data/lib/padrino-admin/generators/admin_page.rb +16 -17
- data/lib/padrino-admin/generators/orm.rb +56 -65
- data/lib/padrino-admin/generators/templates/account/activerecord.rb.tt +11 -11
- data/lib/padrino-admin/generators/templates/account/couchrest.rb.tt +10 -10
- data/lib/padrino-admin/generators/templates/account/datamapper.rb.tt +11 -11
- data/lib/padrino-admin/generators/templates/account/dynamoid.rb.tt +9 -9
- data/lib/padrino-admin/generators/templates/account/minirecord.rb.tt +11 -11
- data/lib/padrino-admin/generators/templates/account/mongoid.rb.tt +16 -16
- data/lib/padrino-admin/generators/templates/account/mongomapper.rb.tt +11 -11
- data/lib/padrino-admin/generators/templates/account/ohm.rb.tt +2 -2
- data/lib/padrino-admin/generators/templates/account/seeds.rb.tt +2 -2
- data/lib/padrino-admin/generators/templates/app/controllers/base.rb.tt +2 -2
- data/lib/padrino-admin/generators/templates/app/controllers/sessions.rb.tt +2 -2
- data/lib/padrino-admin/generators/templates/app.rb.tt +7 -7
- data/lib/padrino-admin/generators/templates/erb/app/layouts/application.erb.tt +10 -9
- data/lib/padrino-admin/generators/templates/erb/app/layouts/error.erb.tt +3 -2
- data/lib/padrino-admin/generators/templates/erb/app/sessions/new.erb.tt +8 -7
- data/lib/padrino-admin/generators/templates/erb/page/_form.erb.tt +7 -7
- data/lib/padrino-admin/generators/templates/erb/page/edit.erb.tt +3 -3
- data/lib/padrino-admin/generators/templates/erb/page/index.erb.tt +12 -12
- data/lib/padrino-admin/generators/templates/erb/page/new.erb.tt +2 -2
- data/lib/padrino-admin/generators/templates/haml/app/base/index.haml.tt +10 -5
- data/lib/padrino-admin/generators/templates/haml/app/layouts/application.haml.tt +14 -14
- data/lib/padrino-admin/generators/templates/haml/app/layouts/error.haml.tt +6 -5
- data/lib/padrino-admin/generators/templates/haml/app/sessions/new.haml.tt +12 -11
- data/lib/padrino-admin/generators/templates/haml/page/_form.haml.tt +7 -7
- data/lib/padrino-admin/generators/templates/haml/page/edit.haml.tt +3 -3
- data/lib/padrino-admin/generators/templates/haml/page/index.haml.tt +14 -14
- data/lib/padrino-admin/generators/templates/haml/page/new.haml.tt +2 -2
- data/lib/padrino-admin/generators/templates/page/controller.rb.tt +25 -25
- data/lib/padrino-admin/generators/templates/slim/app/layouts/application.slim.tt +9 -9
- data/lib/padrino-admin/generators/templates/slim/app/layouts/error.slim.tt +1 -1
- data/lib/padrino-admin/generators/templates/slim/app/sessions/new.slim.tt +6 -6
- data/lib/padrino-admin/generators/templates/slim/page/_form.slim.tt +6 -6
- data/lib/padrino-admin/generators/templates/slim/page/edit.slim.tt +3 -3
- data/lib/padrino-admin/generators/templates/slim/page/index.slim.tt +12 -12
- data/lib/padrino-admin/generators/templates/slim/page/new.slim.tt +2 -2
- data/lib/padrino-admin/helpers/authentication_helpers.rb +7 -7
- data/lib/padrino-admin/helpers/view_helpers.rb +11 -11
- data/lib/padrino-admin.rb +4 -3
- data/padrino-admin.gemspec +18 -18
- data/test/fixtures/sequel.rb +14 -14
- data/test/generators/test_account_model_generator.rb +4 -4
- data/test/generators/test_admin_app_generator.rb +22 -22
- data/test/generators/test_admin_page_generator.rb +30 -30
- data/test/helper.rb +6 -6
- data/test/test_admin_application.rb +114 -116
- data/test/test_locale.rb +3 -3
- metadata +8 -8
|
@@ -12,12 +12,12 @@ module Padrino
|
|
|
12
12
|
class Orm
|
|
13
13
|
attr_reader :klass_name, :klass, :name_plural, :name_singular, :orm, :name_param
|
|
14
14
|
|
|
15
|
-
def initialize(name, orm, columns=nil, column_fields=nil)
|
|
15
|
+
def initialize(name, orm, columns = nil, column_fields = nil)
|
|
16
16
|
name = name.to_s
|
|
17
17
|
@klass_name = name.underscore.camelize
|
|
18
18
|
@klass = @klass_name.constantize rescue nil
|
|
19
|
-
@name_param = name.underscore.gsub(
|
|
20
|
-
@name_singular = name.underscore.gsub(
|
|
19
|
+
@name_param = name.underscore.gsub('/', '_')
|
|
20
|
+
@name_singular = name.underscore.gsub(%r{^.*/}, '') # convert submodules i.e. FooBar::Jank.all # => jank
|
|
21
21
|
@name_plural = @name_singular.pluralize
|
|
22
22
|
@orm = orm.to_sym
|
|
23
23
|
@columns = columns
|
|
@@ -35,62 +35,59 @@ module Padrino
|
|
|
35
35
|
def field_type(type)
|
|
36
36
|
type = :string if type.nil? # couchrest-Hack to avoid the next line to fail
|
|
37
37
|
type = type.to_s.demodulize.downcase.to_sym unless type.is_a?(Symbol)
|
|
38
|
+
|
|
38
39
|
case type
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
40
|
+
when :integer, :float, :decimal then :text_field
|
|
41
|
+
when :string then :text_field
|
|
42
|
+
when :text then :text_area
|
|
43
|
+
when :boolean then :check_box
|
|
44
|
+
else :text_field
|
|
44
45
|
end
|
|
45
46
|
end
|
|
46
47
|
|
|
47
48
|
Column = Struct.new(:name, :type) # for compatibility
|
|
48
49
|
|
|
49
50
|
def columns
|
|
50
|
-
@columns ||=
|
|
51
|
+
@columns ||=
|
|
52
|
+
case orm
|
|
51
53
|
when :activerecord then @klass.columns
|
|
52
54
|
when :minirecord then @klass.columns
|
|
53
55
|
when :datamapper then @klass.properties.map { |p| dm_column(p) }
|
|
54
56
|
when :couchrest then @klass.properties
|
|
55
57
|
when :mongoid then @klass.fields.values.reject { |col| %w[_id _type].include?(col.name) }
|
|
56
|
-
when :mongomapper then @klass.keys.values.reject { |key| key.name ==
|
|
57
|
-
when :sequel then @klass.db_schema.map { |k,v| v[:type] = :text if v[:db_type] =~ /^text/i; Column.new(k, v[:type]) }
|
|
58
|
+
when :mongomapper then @klass.keys.values.reject { |key| key.name == '_id' } # On MongoMapper keys are an hash
|
|
59
|
+
when :sequel then @klass.db_schema.map { |k, v| v[:type] = :text if v[:db_type] =~ /^text/i; Column.new(k, v[:type]) }
|
|
58
60
|
when :ohm then @klass.attributes.map { |a| Column.new(a.to_s, :string) } # ohm has strings
|
|
59
|
-
when :dynamoid then @klass.attributes.map { |k,v| Column.new(k.to_s, v[:type]) }
|
|
61
|
+
when :dynamoid then @klass.attributes.map { |k, v| Column.new(k.to_s, v[:type]) }
|
|
60
62
|
else raise OrmError, "Adapter #{orm} is not yet supported!"
|
|
61
|
-
|
|
63
|
+
end
|
|
62
64
|
end
|
|
63
65
|
|
|
64
|
-
def dm_column(
|
|
65
|
-
case
|
|
66
|
+
def dm_column(property)
|
|
67
|
+
case property
|
|
66
68
|
when DataMapper::Property::Text
|
|
67
|
-
Column.new(
|
|
69
|
+
Column.new(property.name, :text)
|
|
68
70
|
when DataMapper::Property::Boolean
|
|
69
|
-
Column.new(
|
|
71
|
+
Column.new(property.name, :boolean)
|
|
70
72
|
when DataMapper::Property::Integer
|
|
71
|
-
Column.new(
|
|
73
|
+
Column.new(property.name, :integer)
|
|
72
74
|
when DataMapper::Property::Decimal
|
|
73
|
-
Column.new(
|
|
75
|
+
Column.new(property.name, :decimal)
|
|
74
76
|
when DataMapper::Property::Float
|
|
75
|
-
Column.new(
|
|
77
|
+
Column.new(property.name, :float)
|
|
76
78
|
when DataMapper::Property::String
|
|
77
|
-
Column.new(
|
|
78
|
-
else #if all fails, lets assume its
|
|
79
|
-
Column.new(
|
|
79
|
+
Column.new(property.name, :string)
|
|
80
|
+
else # if all fails, lets assume its string-ish
|
|
81
|
+
Column.new(property.name, :string)
|
|
80
82
|
end
|
|
81
83
|
end
|
|
82
84
|
|
|
83
85
|
def column_fields
|
|
84
|
-
excluded_columns = %w[created_at updated_at]
|
|
85
|
-
case orm
|
|
86
|
-
when :mongoid then excluded_columns << '_id'
|
|
87
|
-
else excluded_columns << 'id'
|
|
88
|
-
end
|
|
89
|
-
|
|
86
|
+
excluded_columns = %w[created_at updated_at] << (orm == :mongoid ? '_id' : 'id')
|
|
90
87
|
column_fields = columns.dup
|
|
91
88
|
column_fields.reject! { |column| excluded_columns.include?(column.name.to_s) }
|
|
92
89
|
@column_fields ||= column_fields.map do |column|
|
|
93
|
-
{ :
|
|
90
|
+
{ name: column.name, field_type: field_type(column.type) }
|
|
94
91
|
end
|
|
95
92
|
end
|
|
96
93
|
|
|
@@ -98,16 +95,16 @@ module Padrino
|
|
|
98
95
|
"#{klass_name}.all"
|
|
99
96
|
end
|
|
100
97
|
|
|
101
|
-
def find(params=nil)
|
|
98
|
+
def find(params = nil)
|
|
102
99
|
case orm
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
100
|
+
when :activerecord, :minirecord, :mongomapper, :mongoid, :dynamoid then "#{klass_name}.find(#{params})"
|
|
101
|
+
when :datamapper, :couchrest then "#{klass_name}.get(#{params})"
|
|
102
|
+
when :sequel, :ohm then "#{klass_name}[#{params}]"
|
|
103
|
+
else raise OrmError, "Adapter #{orm} is not yet supported!"
|
|
107
104
|
end
|
|
108
105
|
end
|
|
109
106
|
|
|
110
|
-
def build(params=nil)
|
|
107
|
+
def build(params = nil)
|
|
111
108
|
if params
|
|
112
109
|
"#{klass_name}.new(#{params})"
|
|
113
110
|
else
|
|
@@ -116,54 +113,48 @@ module Padrino
|
|
|
116
113
|
end
|
|
117
114
|
|
|
118
115
|
def save
|
|
119
|
-
|
|
120
|
-
when :sequel then "(@#{name_singular}.save rescue false)"
|
|
121
|
-
else "@#{name_singular}.save"
|
|
122
|
-
end
|
|
116
|
+
orm == :sequel ? "(@#{name_singular}.save rescue false)" : "@#{name_singular}.save"
|
|
123
117
|
end
|
|
124
118
|
|
|
125
|
-
def update_attributes(params=nil)
|
|
119
|
+
def update_attributes(params = nil)
|
|
126
120
|
case orm
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
121
|
+
when :mongomapper, :mongoid, :couchrest, :dynamoid then "@#{name_singular}.update_attributes(#{params})"
|
|
122
|
+
when :activerecord, :minirecord, :datamapper, :ohm then "@#{name_singular}.update(#{params})"
|
|
123
|
+
when :sequel then "@#{name_singular}.modified! && @#{name_singular}.update(#{params})"
|
|
124
|
+
else raise OrmError, "Adapter #{orm} is not yet supported!"
|
|
131
125
|
end
|
|
132
126
|
end
|
|
133
127
|
|
|
134
128
|
def destroy
|
|
135
|
-
|
|
136
|
-
when :ohm then "#{name_singular}.delete"
|
|
137
|
-
else "#{name_singular}.destroy"
|
|
138
|
-
end
|
|
129
|
+
orm == :ohm ? "#{name_singular}.delete" : "#{name_singular}.destroy"
|
|
139
130
|
end
|
|
140
131
|
|
|
141
|
-
def find_by_ids(params=nil)
|
|
132
|
+
def find_by_ids(params = nil)
|
|
142
133
|
case orm
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
134
|
+
when :ohm then "#{klass_name}.fetch(#{params})"
|
|
135
|
+
when :datamapper then "#{klass_name}.all(id: #{params})"
|
|
136
|
+
when :sequel then "#{klass_name}.where(id: #{params})"
|
|
137
|
+
when :mongoid then "#{klass_name}.find(#{params})"
|
|
138
|
+
when :couchrest then "#{klass_name}.all(keys: #{params})"
|
|
139
|
+
when :dynamoid then "#{klass_name}.find(#{params})"
|
|
140
|
+
else find(params)
|
|
150
141
|
end
|
|
151
142
|
end
|
|
152
143
|
|
|
153
|
-
def multiple_destroy(params=nil)
|
|
144
|
+
def multiple_destroy(params = nil)
|
|
154
145
|
case orm
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
146
|
+
when :ohm then "#{params}.each(&:delete)"
|
|
147
|
+
when :sequel then "#{params}.destroy"
|
|
148
|
+
when :datamapper then "#{params}.destroy"
|
|
149
|
+
when :couchrest, :mongoid, :mongomapper, :dynamoid then "#{params}.each(&:destroy)"
|
|
150
|
+
else "#{klass_name}.destroy #{params}"
|
|
160
151
|
end
|
|
161
152
|
end
|
|
162
153
|
|
|
163
154
|
def has_error(field)
|
|
164
155
|
case orm
|
|
165
|
-
|
|
166
|
-
|
|
156
|
+
when :datamapper, :ohm, :sequel then "@#{name_singular}.errors.key?(:#{field}) && @#{name_singular}.errors[:#{field}].count > 0"
|
|
157
|
+
else "@#{name_singular}.errors.include?(:#{field})"
|
|
167
158
|
end
|
|
168
159
|
end
|
|
169
160
|
end
|
|
@@ -3,24 +3,24 @@ class <%= @model_name %> < ActiveRecord::Base
|
|
|
3
3
|
|
|
4
4
|
# Validations
|
|
5
5
|
validates_presence_of :email, :role
|
|
6
|
-
validates_presence_of :password,
|
|
7
|
-
validates_presence_of :password_confirmation,
|
|
8
|
-
validates_length_of :password, :
|
|
9
|
-
validates_confirmation_of :password,
|
|
10
|
-
validates_length_of :email, :
|
|
11
|
-
validates_uniqueness_of :email, :
|
|
12
|
-
validates_format_of :email, :
|
|
13
|
-
validates_format_of :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
14
|
|
|
15
15
|
# Callbacks
|
|
16
|
-
before_save :encrypt_password, :
|
|
16
|
+
before_save :encrypt_password, if: :password_required
|
|
17
17
|
|
|
18
18
|
##
|
|
19
19
|
# This method is for authentication purpose.
|
|
20
20
|
#
|
|
21
21
|
def self.authenticate(email, password)
|
|
22
|
-
account = where(
|
|
23
|
-
account
|
|
22
|
+
account = where('lower(email) = lower(?)', email).first if email.present?
|
|
23
|
+
account if account&.has_password?(password)
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
def has_password?(password)
|
|
@@ -12,24 +12,24 @@ class <%= @model_name %> < CouchRest::Model::Base
|
|
|
12
12
|
|
|
13
13
|
# Validations
|
|
14
14
|
validates_presence_of :email, :role
|
|
15
|
-
validates_presence_of :password,
|
|
16
|
-
validates_presence_of :password_confirmation,
|
|
17
|
-
validates_length_of :password, :
|
|
18
|
-
validates_confirmation_of :password,
|
|
19
|
-
validates_length_of :email, :
|
|
20
|
-
validates_format_of :email, :
|
|
21
|
-
validates_format_of :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
22
|
validate :unique_email_validator
|
|
23
23
|
|
|
24
24
|
# Callbacks
|
|
25
|
-
after_validation :encrypt_password, :
|
|
25
|
+
after_validation :encrypt_password, if: :password_required
|
|
26
26
|
|
|
27
27
|
##
|
|
28
28
|
# This method is for authentication purpose.
|
|
29
29
|
#
|
|
30
30
|
def self.authenticate(email, password)
|
|
31
31
|
account = find_by_email(email)
|
|
32
|
-
account if account
|
|
32
|
+
account if account&.has_password?(password)
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
def has_password?(password)
|
|
@@ -62,6 +62,6 @@ class <%= @model_name %> < CouchRest::Model::Base
|
|
|
62
62
|
# Account with same email in database is this account.
|
|
63
63
|
return if has_key?('_id') && self['_id'] == account['_id']
|
|
64
64
|
|
|
65
|
-
errors.add(:email,
|
|
65
|
+
errors.add(:email, 'is not unique')
|
|
66
66
|
end
|
|
67
67
|
end
|
|
@@ -8,19 +8,19 @@ class <%= @model_name %>
|
|
|
8
8
|
property :name, String
|
|
9
9
|
property :surname, String
|
|
10
10
|
property :email, String
|
|
11
|
-
property :crypted_password, String, :
|
|
11
|
+
property :crypted_password, String, length: 70
|
|
12
12
|
property :role, String
|
|
13
13
|
|
|
14
14
|
# Validations
|
|
15
15
|
validates_presence_of :email, :role
|
|
16
|
-
validates_presence_of :password,
|
|
17
|
-
validates_presence_of :password_confirmation,
|
|
18
|
-
validates_length_of :password, :
|
|
19
|
-
validates_confirmation_of :password,
|
|
20
|
-
validates_length_of :email, :
|
|
21
|
-
validates_uniqueness_of :email, :
|
|
22
|
-
validates_format_of :email, :
|
|
23
|
-
validates_format_of :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
24
|
|
|
25
25
|
# Callbacks
|
|
26
26
|
before :save, :encrypt_password
|
|
@@ -29,8 +29,8 @@ class <%= @model_name %>
|
|
|
29
29
|
# This method is for authentication purpose.
|
|
30
30
|
#
|
|
31
31
|
def self.authenticate(email, password)
|
|
32
|
-
account = first(:
|
|
33
|
-
account
|
|
32
|
+
account = first(conditions: ['lower(email) = lower(?)', email]) if email.present?
|
|
33
|
+
account if account&.has_password?(password)
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
##
|
|
@@ -11,23 +11,23 @@ class <%= @model_name %>
|
|
|
11
11
|
|
|
12
12
|
# Validations
|
|
13
13
|
validates_presence_of :email, :role
|
|
14
|
-
validates_presence_of :password,
|
|
15
|
-
validates_presence_of :password_confirmation,
|
|
16
|
-
validates_length_of :password, :
|
|
17
|
-
validates_confirmation_of :password,
|
|
18
|
-
validates_length_of :email, :
|
|
19
|
-
validates_format_of :email, :
|
|
20
|
-
validates_format_of :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_format_of :email, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
|
|
20
|
+
validates_format_of :role, with: /[A-Za-z]/
|
|
21
21
|
|
|
22
22
|
# Callbacks
|
|
23
|
-
before_save :encrypt_password, :
|
|
23
|
+
before_save :encrypt_password, if: :password_required
|
|
24
24
|
|
|
25
25
|
##
|
|
26
26
|
# This method is for authentication purpose.
|
|
27
27
|
#
|
|
28
28
|
def self.authenticate(email, password)
|
|
29
29
|
account = find_by_email(email) if email.present?
|
|
30
|
-
account
|
|
30
|
+
account if account&.has_password?(password)
|
|
31
31
|
end
|
|
32
32
|
|
|
33
33
|
##
|
|
@@ -6,24 +6,24 @@ class <%= @model_name %> < ActiveRecord::Base
|
|
|
6
6
|
|
|
7
7
|
# Validations
|
|
8
8
|
validates_presence_of :email, :role
|
|
9
|
-
validates_presence_of :password,
|
|
10
|
-
validates_presence_of :password_confirmation,
|
|
11
|
-
validates_length_of :password, :
|
|
12
|
-
validates_confirmation_of :password,
|
|
13
|
-
validates_length_of :email, :
|
|
14
|
-
validates_uniqueness_of :email, :
|
|
15
|
-
validates_format_of :email, :
|
|
16
|
-
validates_format_of :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
17
|
|
|
18
18
|
# Callbacks
|
|
19
|
-
before_save :encrypt_password, :
|
|
19
|
+
before_save :encrypt_password, if: :password_required
|
|
20
20
|
|
|
21
21
|
##
|
|
22
22
|
# This method is for authentication purpose.
|
|
23
23
|
#
|
|
24
24
|
def self.authenticate(email, password)
|
|
25
|
-
account = where(
|
|
26
|
-
account
|
|
25
|
+
account = where('lower(email) = lower(?)', email).first if email.present?
|
|
26
|
+
account if account&.has_password?(password)
|
|
27
27
|
end
|
|
28
28
|
|
|
29
29
|
def has_password?(password)
|
|
@@ -3,32 +3,32 @@ class <%= @model_name %>
|
|
|
3
3
|
attr_accessor :password, :password_confirmation
|
|
4
4
|
|
|
5
5
|
# Fields
|
|
6
|
-
field :name, :
|
|
7
|
-
field :surname, :
|
|
8
|
-
field :email, :
|
|
9
|
-
field :crypted_password, :
|
|
10
|
-
field :role, :
|
|
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
11
|
|
|
12
12
|
# Validations
|
|
13
13
|
validates_presence_of :email, :role
|
|
14
|
-
validates_presence_of :password,
|
|
15
|
-
validates_presence_of :password_confirmation,
|
|
16
|
-
validates_length_of :password, :
|
|
17
|
-
validates_confirmation_of :password,
|
|
18
|
-
validates_length_of :email, :
|
|
19
|
-
validates_uniqueness_of :email, :
|
|
20
|
-
validates_format_of :email, :
|
|
21
|
-
validates_format_of :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
22
|
|
|
23
23
|
# Callbacks
|
|
24
|
-
before_save :encrypt_password, :
|
|
24
|
+
before_save :encrypt_password, if: :password_required
|
|
25
25
|
|
|
26
26
|
##
|
|
27
27
|
# This method is for authentication purpose.
|
|
28
28
|
#
|
|
29
29
|
def self.authenticate(email, password)
|
|
30
|
-
account = where(:
|
|
31
|
-
account
|
|
30
|
+
account = where(email: /#{Object::Regexp.escape(email)}/i).first if email.present?
|
|
31
|
+
account if account&.has_password?(password)
|
|
32
32
|
end
|
|
33
33
|
|
|
34
34
|
##
|
|
@@ -11,24 +11,24 @@ class <%= @model_name %>
|
|
|
11
11
|
|
|
12
12
|
# Validations
|
|
13
13
|
validates_presence_of :email, :role
|
|
14
|
-
validates_presence_of :password,
|
|
15
|
-
validates_presence_of :password_confirmation,
|
|
16
|
-
validates_length_of :password, :
|
|
17
|
-
validates_confirmation_of :password,
|
|
18
|
-
validates_length_of :email, :
|
|
19
|
-
validates_uniqueness_of :email, :
|
|
20
|
-
validates_format_of :email, :
|
|
21
|
-
validates_format_of :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
22
|
|
|
23
23
|
# Callbacks
|
|
24
|
-
before_save :encrypt_password, :
|
|
24
|
+
before_save :encrypt_password, if: :password_required
|
|
25
25
|
|
|
26
26
|
##
|
|
27
27
|
# This method is for authentication purpose.
|
|
28
28
|
#
|
|
29
29
|
def self.authenticate(email, password)
|
|
30
|
-
account = first(:
|
|
31
|
-
account
|
|
30
|
+
account = first(email: /#{Regexp.escape(email)}/i) if email.present?
|
|
31
|
+
account if account&.has_password?(password)
|
|
32
32
|
end
|
|
33
33
|
|
|
34
34
|
def has_password?(password)
|
|
@@ -36,7 +36,7 @@ class Account < Ohm::Model
|
|
|
36
36
|
#
|
|
37
37
|
def self.authenticate(email, password)
|
|
38
38
|
account = with(:email, email) if email.present?
|
|
39
|
-
account
|
|
39
|
+
account if account&.has_password?(password)
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
##
|
|
@@ -50,7 +50,7 @@ class Account < Ohm::Model
|
|
|
50
50
|
# This method is used by Admin Sessions Controller for login bypass.
|
|
51
51
|
#
|
|
52
52
|
def self.first
|
|
53
|
-
first_id = key[:all].sort(:
|
|
53
|
+
first_id = key[:all].sort(order: "asc", limit: [0, 1]).first
|
|
54
54
|
self[first_id] if first_id
|
|
55
55
|
end
|
|
56
56
|
|
|
@@ -6,11 +6,11 @@
|
|
|
6
6
|
# shell.say name
|
|
7
7
|
#
|
|
8
8
|
email = shell.ask "Which email do you want use for logging into admin?"
|
|
9
|
-
password = shell.ask "Tell me the password to use:", :
|
|
9
|
+
password = shell.ask "Tell me the password to use:", echo: false
|
|
10
10
|
|
|
11
11
|
shell.say ""
|
|
12
12
|
|
|
13
|
-
account = <%= @model_name %>.new(:
|
|
13
|
+
account = <%= @model_name %>.new(email: email, name: "Foo", surname: "Bar", password: password, password_confirmation: password, role: "admin")
|
|
14
14
|
|
|
15
15
|
if account.valid?
|
|
16
16
|
account.save
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<%= @app_name %>::<%= @admin_name %>.controllers :sessions do
|
|
2
2
|
get :new do
|
|
3
|
-
render
|
|
3
|
+
render '/sessions/new', nil, layout: false
|
|
4
4
|
end
|
|
5
5
|
|
|
6
6
|
post :create do
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
else
|
|
19
19
|
params[:email] = h(params[:email])
|
|
20
20
|
flash.now[:error] = pat('login.error')
|
|
21
|
-
render
|
|
21
|
+
render '/sessions/new', nil, layout: false
|
|
22
22
|
end
|
|
23
23
|
end
|
|
24
24
|
|
|
@@ -14,10 +14,10 @@ module <%= @app_name %>
|
|
|
14
14
|
# set :dump_errors, true # Exception backtraces are written to STDERR (default for production/development)
|
|
15
15
|
# set :show_exceptions, true # Shows a stack trace in browser (default for development)
|
|
16
16
|
# set :logging, true # Logging in STDOUT for development and file for production (default only for development)
|
|
17
|
-
# set :public_folder,
|
|
17
|
+
# set :public_folder, 'foo/bar' # Location for static assets (default root/public)
|
|
18
18
|
# set :reload, false # Reload application files (default in development)
|
|
19
|
-
# set :default_builder,
|
|
20
|
-
# set :locale_path,
|
|
19
|
+
# set :default_builder, 'foo' # Set a custom form builder (default 'StandardFormBuilder')
|
|
20
|
+
# set :locale_path, 'bar' # Set path for I18n translations (default your_app/locales)
|
|
21
21
|
# disable :sessions # Disabled sessions by default (enable if needed)
|
|
22
22
|
# disable :flash # Disables sinatra-flash (enabled by default if Sinatra::Flash is defined)
|
|
23
23
|
# layout :my_layout # Layout can be in views/layouts/foo.ext or views/foo.ext (default :application)
|
|
@@ -37,9 +37,9 @@ module <%= @app_name %>
|
|
|
37
37
|
access_control.roles_for :admin do |role|
|
|
38
38
|
end
|
|
39
39
|
|
|
40
|
-
# Custom error management
|
|
41
|
-
error(403) { @title =
|
|
42
|
-
error(404) { @title =
|
|
43
|
-
error(500) { @title =
|
|
40
|
+
# Custom error management
|
|
41
|
+
error(403) { @title = 'Error 403'; render('errors/403', layout: :error) }
|
|
42
|
+
error(404) { @title = 'Error 404'; render('errors/404', layout: :error) }
|
|
43
|
+
error(500) { @title = 'Error 500'; render('errors/500', layout: :error) }
|
|
44
44
|
end
|
|
45
45
|
end
|
|
@@ -12,15 +12,16 @@
|
|
|
12
12
|
<link href='http://fonts.googleapis.com/css?family=Varela' rel=stylesheet />
|
|
13
13
|
<%%= stylesheet_link_tag 'bootstrap', 'application' %>
|
|
14
14
|
</head>
|
|
15
|
+
|
|
15
16
|
<body>
|
|
16
17
|
<div class="navbar navbar-fixed-top">
|
|
17
18
|
<div class=navbar-inner>
|
|
18
19
|
<div class=container>
|
|
19
|
-
<%%= link_to 'Padrino', url(:base, :index), :
|
|
20
|
+
<%%= link_to 'Padrino', url(:base, :index), class: 'navbar-brand', title: 'Padrino Admin' %>
|
|
20
21
|
<ul class="nav navbar-nav pull-right">
|
|
21
|
-
<li class=navbar-edit-account><%%= link_to tag_icon(:user), url(:<%= @model_plural %>, :edit, :
|
|
22
|
+
<li class=navbar-edit-account><%%= link_to tag_icon(:user), url(:<%= @model_plural %>, :edit, id: current_account.id), title: pat(:profile), class: 'navbar-nav-link' %></li>
|
|
22
23
|
<li class=navbar-logout>
|
|
23
|
-
<%%= button_to(:logout, url(:sessions, :destroy), :
|
|
24
|
+
<%%= button_to(:logout, url(:sessions, :destroy), method: :delete, class: 'navbar-nav-form', submit_options: { type: :submit, title: pat(:logout), class: 'navbar-nav-form-link' }) { tag_icon('power-off') } %>
|
|
24
25
|
</li>
|
|
25
26
|
</ul>
|
|
26
27
|
|
|
@@ -41,7 +42,7 @@
|
|
|
41
42
|
|
|
42
43
|
<div class='container main'>
|
|
43
44
|
<div class='main-wrapper'>
|
|
44
|
-
<%%= {:
|
|
45
|
+
<%%= { error: 'danger', warning: 'warning', success: 'success', notice: 'info' }.map { |type, class_name| flash_tag(type, class: "alert alert-#{class_name} fade in", bootstrap: true) }.join.html_safe %>
|
|
45
46
|
<div class='row'><%%= yield %></div>
|
|
46
47
|
<div class='main-wrapper-push'></div>
|
|
47
48
|
</div>
|
|
@@ -51,14 +52,14 @@
|
|
|
51
52
|
<div class='footer-wrapper container'>
|
|
52
53
|
<p class='pull-left'>Copyright © <%= Time.now.year %> Your Site - Powered by Padrino v.<%= Padrino.version %></p>
|
|
53
54
|
<ul class='pull-right footer-links'>
|
|
54
|
-
<li><%%= link_to tag_icon(:home, 'web'), 'http://www.padrinorb.com', :
|
|
55
|
-
<li><%%= link_to tag_icon(:heart, 'blog'), 'http://www.padrinorb.com/blog', :
|
|
56
|
-
<li><%%= link_to tag_icon(:github, 'code'), 'https://github.com/padrino/padrino-framework', :
|
|
57
|
-
<li><%%= link_to tag_icon(:twitter, 'twitter'), 'http://twitter.com/padrinorb', :
|
|
55
|
+
<li><%%= link_to tag_icon(:home, 'web'), 'http://www.padrinorb.com', target: :_blank, class: 'footer-links-link' %></li>
|
|
56
|
+
<li><%%= link_to tag_icon(:heart, 'blog'), 'http://www.padrinorb.com/blog', target: :_blank, class: 'footer-links-link' %></li>
|
|
57
|
+
<li><%%= link_to tag_icon(:github, 'code'), 'https://github.com/padrino/padrino-framework', target: :_blank, class: 'footer-links-link' %></li>
|
|
58
|
+
<li><%%= link_to tag_icon(:twitter, 'twitter'), 'http://twitter.com/padrinorb', target: :_blank, class: 'footer-links-link' %></li>
|
|
58
59
|
</ul>
|
|
59
60
|
</div>
|
|
60
61
|
</footer>
|
|
61
62
|
|
|
62
|
-
<%%= javascript_include_tag 'jquery-1.11.0.min', (Padrino.env == :production ? 'bootstrap/bootstrap.min' : %w[bootstrap/affix bootstrap/alert bootstrap/button bootstrap/carousel bootstrap/collapse bootstrap/dropdown
|
|
63
|
+
<%%= javascript_include_tag 'jquery-1.11.0.min', (Padrino.env == :production ? 'bootstrap/bootstrap.min' : %w[bootstrap/affix bootstrap/alert bootstrap/button bootstrap/carousel bootstrap/collapse bootstrap/dropdown bootstrap/tooltip bootstrap/transition bootstrap/modal bootstrap/popover bootstrap/scrollspy bootstrap/tab]), :application %>
|
|
63
64
|
</body>
|
|
64
65
|
</html>
|