padrino-admin 0.18.1 → 0.18.3

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.
Files changed (29) hide show
  1. checksums.yaml +4 -4
  2. data/lib/padrino-admin/access_control.rb +26 -4
  3. data/lib/padrino-admin/generators/actions.rb +1 -0
  4. data/lib/padrino-admin/generators/admin_app.rb +36 -17
  5. data/lib/padrino-admin/generators/admin_page.rb +2 -1
  6. data/lib/padrino-admin/generators/keep_files.rb +56 -0
  7. data/lib/padrino-admin/generators/orm.rb +16 -2
  8. data/lib/padrino-admin/generators/templates/assets/stylesheets/application.css +0 -3
  9. data/lib/padrino-admin/generators/templates/erb/app/layouts/application.erb.tt +0 -5
  10. data/lib/padrino-admin/generators/templates/erb/app/layouts/error.erb.tt +0 -5
  11. data/lib/padrino-admin/generators/templates/erb/app/sessions/new.erb.tt +0 -4
  12. data/lib/padrino-admin/generators/templates/erb/page/index.erb.tt +2 -2
  13. data/lib/padrino-admin/generators/templates/haml/app/layouts/application.haml.tt +0 -5
  14. data/lib/padrino-admin/generators/templates/haml/app/layouts/error.haml.tt +0 -5
  15. data/lib/padrino-admin/generators/templates/haml/app/sessions/new.haml.tt +0 -4
  16. data/lib/padrino-admin/generators/templates/haml/page/index.haml.tt +2 -2
  17. data/lib/padrino-admin/generators/templates/page/controller.rb.tt +0 -3
  18. data/lib/padrino-admin/generators/templates/slim/app/layouts/application.slim.tt +0 -5
  19. data/lib/padrino-admin/generators/templates/slim/app/layouts/error.slim.tt +0 -5
  20. data/lib/padrino-admin/generators/templates/slim/app/sessions/new.slim.tt +0 -4
  21. data/lib/padrino-admin/generators/templates/slim/page/index.slim.tt +2 -2
  22. data/lib/padrino-admin/helpers/authentication_helpers.rb +3 -1
  23. data/lib/padrino-admin.rb +21 -1
  24. data/padrino-admin.gemspec +8 -3
  25. data/test/generators/test_admin_app_generator.rb +54 -0
  26. data/test/generators/test_admin_page_generator.rb +126 -0
  27. data/test/generators/test_admin_rerun_generator.rb +244 -0
  28. data/test/test_admin_application.rb +305 -0
  29. metadata +15 -9
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 281388666c8cb872b46f3ce068940e8718006d27375c7a81b5281e09e8a250de
4
- data.tar.gz: e3b4d990cc8c9d48954531eeb3234efadc0d46ab2a63fdf34cce3f1e8ce218ac
3
+ metadata.gz: 4bb555e27244a446b1433ec201e00a5ab6bb895563bb709f8cb6e3b72368cb0a
4
+ data.tar.gz: 299c5bff7ddf533337f11378edba709f94909af649885d126aeba68d59e8ceac
5
5
  SHA512:
6
- metadata.gz: 7c2b5c039a5c8716aaeb1f4e6a1aad7bf61aeee5994cb567e425a3580050d282e473e033037a603ceb85feeef1a84fcb8d0f3d056e5916d97bd2bcec54398e79
7
- data.tar.gz: e58b0d302e3d6ff10a2ae6866e55640819512d6309e06a595138a91ee61e3ff1789abd898dfe233245d97a49dcf366d6014d5d5b8f5ba9d1723c6aa0ebbebb97
6
+ metadata.gz: da7e04c9e6b9377d68424fcf0b0f01eb7656b5bac561cb561941756a8a7b3ab6dfafd462e61277e402a404ab3ff06157b7b9d13f0c2648ffbadf28a48c0e3e19
7
+ data.tar.gz: 10b046f9d168867be3e1cdef1b3324cc36eec09779f2d34d9defd00474fd82d1cb8b6f3e41840301711340008997026933c35c903208ee7205a5a9ffaac1eb24
@@ -36,6 +36,10 @@ module Padrino
36
36
  # This base access control class where roles are defined as are authorizations.
37
37
  #
38
38
  class Base
39
+ # How many forms of a request path deny Regexps are checked against at most; a path with more
40
+ # is denied (deny_forms).
41
+ MAX_DENY_FORMS = 64
42
+
39
43
  def initialize
40
44
  @roles, @authorizations, @project_modules = [], [], []
41
45
  end
@@ -104,8 +108,9 @@ module Padrino
104
108
  #
105
109
  def allowed?(account = nil, path = nil)
106
110
  path = '/' if path.nil? || path.empty?
107
- # Compare the decoded path; scrub invalid UTF-8 ('/%ff') so Regexp rules can match it.
108
- path = Rack::Utils.unescape_path(path.b).force_encoding(Encoding::UTF_8).scrub
111
+ # Allow rules see the decoded path the router matches ('%2F' stays inside its segment); deny
112
+ # rules see every form the router may serve it as (deny_forms).
113
+ paths = Padrino::Routing.path_forms(path)
109
114
  role = account.role.to_sym rescue nil
110
115
  authorizations = @authorizations.find_all { |auth| auth.roles.include?(:any) }
111
116
  allowed_paths = authorizations.map(&:allowed).flatten.uniq
@@ -120,13 +125,30 @@ module Padrino
120
125
  denied_paths += authorizations.map(&:allowed).flatten.uniq # remove paths explicitly allowed for other roles
121
126
  denied_paths += authorizations.map(&:denied).flatten.uniq # remove paths explicitly denied to other roles
122
127
  end
123
- return true if allowed_paths.any? { |p| path_within?(path, p) }
124
- return false if denied_paths.any? { |p| path.start_with?(p) } # loose on purpose: fails closed
128
+ # A path with a newline satisfies no allow rule.
129
+ return denied_paths.empty? if paths.any? { |form| form.include?("\n") }
130
+ return true if allowed_paths.any? { |p| path_within?(paths.first, p) }
131
+ return true if denied_paths.empty?
132
+
133
+ forms = denied_paths.any?(Regexp) ? deny_forms(paths) : paths
134
+ return false unless forms
135
+ return false if denied_paths.any? { |p| forms.any? { |form| form.start_with?(p) } } # loose on purpose: fails closed
125
136
  true
126
137
  end
127
138
 
128
139
  private
129
140
 
141
+ # The forms of the path (Routing.path_forms), without its trailing slashes, and without a
142
+ # format after a dot of its last segment, as a route may serve any of those ('/billing.json'
143
+ # and '/billing/' from '/billing'). Other delimiters start a different page ('/billing-day').
144
+ # A String rule needs none of them: each is a prefix of a decoded form, which start_with?
145
+ # already covers. Returns nil for a path with more than MAX_DENY_FORMS forms, so that it is
146
+ # denied rather than checked in part.
147
+ def deny_forms(paths)
148
+ forms = Padrino::Routing.served_forms(paths).first(MAX_DENY_FORMS + 1)
149
+ forms unless forms.size > MAX_DENY_FORMS
150
+ end
151
+
130
152
  # An allowed path covers itself, its sub-paths and its formats ('/posts' allows '/posts',
131
153
  # '/posts/1' and '/posts.json'), but not unrelated paths that merely start with it ('/posts_admin').
132
154
  # A Regexp matches at the start of the path, as on the deny side.
@@ -69,6 +69,7 @@ module Padrino
69
69
  #
70
70
  # @api public
71
71
  def fetch_app_name(app = 'app')
72
+ check_app_existence(app)
72
73
  app_path = destination_root(app, 'app.rb')
73
74
  @app_name ||= File.read(app_path).scan(/module\s(.*?)\n/).flatten[0]
74
75
  end
@@ -16,6 +16,7 @@ module Padrino
16
16
  include Thor::Actions
17
17
  include Padrino::Generators::Actions
18
18
  include Padrino::Generators::Admin::Actions
19
+ include Padrino::Generators::Admin::KeepFiles
19
20
 
20
21
  # Look for custom template files in a generators folder under the project root.
21
22
  def source_paths
@@ -57,26 +58,35 @@ module Padrino
57
58
  @admin_name = options[:admin_name].classify
58
59
  @admin_path = options[:admin_name].underscore
59
60
 
61
+ # Setup Admin Model
62
+ @model_name = options[:admin_model].classify
63
+ @model_singular = @model_name.underscore
64
+ @model_plural = @model_singular.pluralize
65
+ model_path = destination_root('models', "#{@model_singular}.rb")
66
+
67
+ # A plain app (g app admin) or a missing one: an admin app has access control once generated.
68
+ first_install = !line_in_file?(destination_root(@admin_path, 'app.rb'), /Padrino::Admin::AccessControl/)
69
+
70
+ stop_for_model_without_login(model_path) if first_install && !options[:destroy]
71
+
60
72
  store_component_choice(:admin_renderer, tmp_ext)
61
73
 
62
74
  self.behavior = :revoke if options[:destroy]
63
75
 
64
76
  empty_directory destination_root(@admin_path)
65
77
 
66
- # Setup Admin Model
67
- @model_name = options[:admin_model].classify
68
- @model_singular = @model_name.underscore
69
- @model_plural = @model_singular.pluralize
70
-
71
78
  directory 'templates/app', destination_root(@admin_path)
72
79
  directory 'templates/assets', destination_root('public', @admin_path)
73
80
  template 'templates/app.rb.tt', destination_root("#{@admin_path}/app.rb")
74
- inject_into_file destination_root('config/apps.rb'), <<~RUBY.prepend("\n"), before: %r{^Padrino.mount.*\.to\('/'\)$}
75
- Padrino.mount('#{@app_name}::#{@admin_name}', app_file: Padrino.root('#{@admin_path}/app.rb')).to('/#{@admin_path}')
76
- RUBY
81
+ mounted = /^\s*Padrino\.mount\W+#{@app_name}::#{@admin_name}\b/
82
+ if options[:destroy] || !line_in_file?(destination_root('config/apps.rb'), mounted)
83
+ inject_into_file destination_root('config/apps.rb'), <<~RUBY.prepend("\n"), before: %r{^Padrino.mount.*\.to\('/'\)$}
84
+ Padrino.mount('#{@app_name}::#{@admin_name}', app_file: Padrino.root('#{@admin_path}/app.rb')).to('/#{@admin_path}')
85
+ RUBY
86
+ end
77
87
 
78
- unless options[:destroy]
79
- insert_middleware 'ConnectionPoolManagement', @admin_path if orm == :activerecord
88
+ unless options[:destroy] || orm != :activerecord || line_in_file?(destination_root(@admin_path, 'app.rb'), /^\s*use[\s(]+ConnectionPoolManagement\b/)
89
+ insert_middleware 'ConnectionPoolManagement', @admin_path
80
90
  end
81
91
 
82
92
  params = [
@@ -87,7 +97,7 @@ module Padrino
87
97
  params << '-d' if options[:destroy]
88
98
 
89
99
  # An existing model is reused: the model generator would refuse it and exit 1.
90
- model_exists = already_exists?(@model_name, fetch_component_choice(:namespace))
100
+ model_exists = already_exists?(@model_name, fetch_component_choice(:namespace)) || File.exist?(model_path)
91
101
  Padrino::Generators::Model.start(params) if options[:destroy] || !model_exists
92
102
  column = Struct.new(:name, :type)
93
103
  columns = %i[id name surname email].map { |col| column.new(col) }
@@ -100,6 +110,9 @@ module Padrino
100
110
  { name: :role, field_type: :text_field }
101
111
  ]
102
112
 
113
+ controller = destination_root("#{@admin_path}/controllers/#{@model_plural}.rb")
114
+ controller_before = File.binread(controller) if File.exist?(controller)
115
+
103
116
  unless options[:destroy]
104
117
  admin_app = Padrino::Generators::AdminPage.new([@model_singular], root: options[:root], destroy: options[:destroy], admin_model: @model_singular, admin_name: options[:admin_name])
105
118
  admin_app.default_orm = Padrino::Admin::Generators::Orm.new(@model_singular, orm, columns, column_fields)
@@ -107,10 +120,14 @@ module Padrino
107
120
  end
108
121
 
109
122
  # TODO: See this, there's something wrong it's not being applied properly or something because test_account_model_generator last test fails.
110
- template "templates/account/#{orm}.rb.tt", destination_root('models', "#{@model_singular}.rb"), force: true
123
+ template "templates/account/#{orm}.rb.tt", model_path, force: !model_exists
111
124
 
125
+ # Seeds of the admin model stay in place; moving others aside never replaces an earlier move.
112
126
  seed_destination = destination_root('db', 'seeds.rb')
113
- run "mv #{seed_destination} #{destination_root('db/seeds.old')}" if File.exist?(seed_destination)
127
+ seed_backup = destination_root('db/seeds.old')
128
+ if first_install && File.exist?(seed_destination) && !File.exist?(seed_backup) && !line_in_file?(seed_destination, /\b#{@model_name}\b/)
129
+ run "mv #{seed_destination} #{seed_backup}"
130
+ end
114
131
  template 'templates/account/seeds.rb.tt', seed_destination
115
132
 
116
133
  empty_directory destination_root("#{@admin_path}/controllers")
@@ -129,19 +146,21 @@ module Padrino
129
146
  template "templates/#{ext}/app/errors/404.#{ext}.tt", destination_root("#{@admin_path}/views/errors/404.#{ext}")
130
147
  template "templates/#{ext}/app/errors/500.#{ext}.tt", destination_root("#{@admin_path}/views/errors/500.#{ext}")
131
148
 
149
+ gemfile = destination_root('Gemfile')
132
150
  unless options[:destroy]
133
- add_project_module @model_plural
134
- require_dependencies('bcrypt')
151
+ add_missing_project_module @model_plural
152
+ require_dependencies('bcrypt') unless line_in_file?(gemfile, /^\s*gem[\s(]+['"]bcrypt['"]/)
135
153
  end
136
154
 
137
- require_dependencies 'activesupport', version: '>= 3.1'
155
+ require_dependencies 'activesupport', version: '>= 3.1' if options[:destroy] || !line_in_file?(gemfile, /^\s*gem[\s(]+['"]activesupport['"]/)
138
156
 
139
157
  # A nicer select box.
140
158
  # TODO FIXME This doesn't make much sense in here. Review.
141
159
  # gsub_file destination_root("admin/views/#{@model_plural}/_form.#{ext}"), "f.text_field :role, class: :text_field", "f.select :role, options: access_control.roles"
142
160
 
143
161
  # Destroy account only if not logged in.
144
- gsub_file destination_root("#{@admin_path}/controllers/#{@model_plural}.rb"), "if #{@model_singular}.destroy", "if #{@model_singular} != current_account && #{@model_singular}.destroy"
162
+ controller_after = File.binread(controller) if File.exist?(controller)
163
+ gsub_file controller, "if #{@model_singular}.destroy", "if #{@model_singular} != current_account && #{@model_singular}.destroy" if controller_after != controller_before
145
164
  return if self.behavior == :revoke
146
165
 
147
166
  instructions = []
@@ -18,6 +18,7 @@ module Padrino
18
18
  include Thor::Actions
19
19
  include Padrino::Generators::Actions
20
20
  include Padrino::Generators::Admin::Actions
21
+ include Padrino::Generators::Admin::KeepFiles
21
22
 
22
23
  # Look for custom template files in a generators folder under the project root.
23
24
  def source_paths
@@ -66,7 +67,7 @@ module Padrino
66
67
  template "templates/#{ext}/page/index.#{ext}.tt", destination_root("#{@admin_path}/views/#{@orm.name_plural}/index.#{ext}")
67
68
  template "templates/#{ext}/page/new.#{ext}.tt", destination_root("#{@admin_path}/views/#{@orm.name_plural}/new.#{ext}")
68
69
 
69
- options[:destroy] ? remove_project_module(@orm.name_plural) : add_project_module(@orm.name_plural)
70
+ options[:destroy] ? remove_project_module(@orm.name_plural) : add_missing_project_module(@orm.name_plural)
70
71
  end
71
72
  else
72
73
  exit_outside_app_root!
@@ -0,0 +1,56 @@
1
+ module Padrino
2
+ module Generators
3
+ module Admin
4
+ ##
5
+ # Keeps an existing file when its overwrite prompt is left unanswered.
6
+ #
7
+ module KeepFiles
8
+ ##
9
+ # Answers no at the end of input (no terminal, empty pipe), which Thor takes as a yes.
10
+ # Other answers, including piped ones, work as usual.
11
+ #
12
+ module Shell
13
+ def file_collision(destination)
14
+ @resolving_file_collision = true
15
+ super
16
+ ensure
17
+ @resolving_file_collision = false
18
+ end
19
+
20
+ def ask(statement, *args)
21
+ answer = super
22
+ answer.nil? && @resolving_file_collision ? 'n' : answer
23
+ end
24
+ end
25
+
26
+ def shell
27
+ super.extend(Shell)
28
+ end
29
+
30
+ ##
31
+ # Tells whether the file exists and has a line matching the pattern.
32
+ #
33
+ def line_in_file?(path, pattern)
34
+ File.exist?(path) && File.binread(path).match?(pattern)
35
+ end
36
+
37
+ ##
38
+ # Adds the access_control permission unless the app has one for the controller, edited or not.
39
+ #
40
+ def add_missing_project_module(controller)
41
+ add_project_module(controller) unless line_in_file?(destination_root("#{@admin_path}/app.rb"), /^\s*role\.project_module[\s(]+:#{controller}\b/)
42
+ end
43
+
44
+ ##
45
+ # Stops when the admin can't log in with an existing model and nobody is left to answer whether to replace it.
46
+ #
47
+ def stop_for_model_without_login(path)
48
+ return if !File.exist?(path) || line_in_file?(path, /\bauthenticate\b/) || $stdin.tty? || !$stdin.eof?
49
+
50
+ raise Thor::Error, "#{path} has no authenticate method and no input is left to confirm replacing it. " \
51
+ 'Add the method to the model, or run the generator in a terminal to choose whether to replace the file.'
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -22,7 +22,7 @@ module Padrino
22
22
  def initialize(name, orm, columns = nil, column_fields = nil)
23
23
  name = name.to_s
24
24
  @klass_name = name.underscore.camelize
25
- @klass = @klass_name.constantize rescue nil
25
+ @klass = load_klass
26
26
  @name_param = name.underscore.gsub('/', '_')
27
27
  @name_singular = name.underscore.gsub(%r{^.*/}, '') # convert submodules i.e. FooBar::Jank.all # => jank
28
28
  @name_plural = @name_singular.pluralize
@@ -35,6 +35,7 @@ module Padrino
35
35
  # needed when reading the model's columns to generate a page, so destroying a page skips it.
36
36
  def check_model_orm
37
37
  return unless @columns.nil?
38
+ raise OrmError, "Model '#{klass_name}' could not be loaded#{' because the project failed to load' if @boot_failed}!\n#{@load_error.class}: #{@load_error.message}" if @load_error
38
39
  raise OrmError, "Model '#{klass_name}' could not be found!\nPerhaps you would like to run 'bundle exec padrino g model #{klass_name}' to create it first?" if @klass.nil?
39
40
 
40
41
  base = MODEL_BASES[orm]
@@ -49,7 +50,7 @@ module Padrino
49
50
 
50
51
  def field_type(type)
51
52
  type = :string if type.nil?
52
- type = type.to_s.demodulize.downcase.to_sym unless type.is_a?(Symbol)
53
+ type = Padrino::Inflections.demodulize(type).downcase.to_sym unless type.is_a?(Symbol)
53
54
 
54
55
  case type
55
56
  when :integer, :float, :decimal then :text_field
@@ -140,6 +141,19 @@ module Padrino
140
141
  else "@#{name_singular}.errors.include?(:#{field})"
141
142
  end
142
143
  end
144
+
145
+ private
146
+
147
+ # Returns nil when the model is not defined. A model that failed to load keeps its error in
148
+ # @load_error, whether looking it up raised it or loading the project (config/boot.rb) did.
149
+ def load_klass
150
+ @klass_name.constantize
151
+ rescue StandardError => e
152
+ missing = Padrino::Admin.missing_constant?(e, @klass_name)
153
+ @load_error = missing ? Padrino::Generators.boot_error : e
154
+ @boot_failed = missing && !@load_error.nil?
155
+ nil
156
+ end
143
157
  end
144
158
  end
145
159
  end
@@ -139,7 +139,6 @@ footer p {
139
139
 
140
140
  /* -- Begin Errors -- */
141
141
  .custom-error {
142
- font-family: 'Varela', sans-serif;
143
142
  font-size: 1.5em;
144
143
  width: 500px;
145
144
  margin: 100px auto 0;
@@ -218,7 +217,6 @@ footer p {
218
217
  margin-top: 10px;
219
218
  height: 20px; /* Bootstrap 3.4 makes it 50px, which pushes the navbar down */
220
219
  padding: 0px 15px;
221
- font-family: 'Varela', sans-serif;
222
220
  text-transform: uppercase;
223
221
  font-size: 2.5em;
224
222
  line-height: 1em;
@@ -318,7 +316,6 @@ footer p {
318
316
  /* --- End Application::List --- */
319
317
  /* --- Begin Application::Base --- */
320
318
  .base-text {
321
- font-family: 'Varela', sans-serif;
322
319
  width: 730px;
323
320
  margin: 40px auto 0;
324
321
  font-size: 1.45em;
@@ -1,15 +1,10 @@
1
1
  <!DOCTYPE html>
2
2
  <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
3
3
  <head>
4
- <!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
5
- <!--[if lt IE 9]>
6
- <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
7
- <![endif]-->
8
4
  <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
9
5
  <meta name=viewport content='width=device-width,initial-scale=1' />
10
6
  <title><%%= @title.present? ? "#{@title} | Padrino Admin" : "Padrino Admin" %></title>
11
7
  <%%= favicon_tag "favicon.ico" %>
12
- <link href='http://fonts.googleapis.com/css?family=Varela' rel=stylesheet />
13
8
  <%%= stylesheet_link_tag 'bootstrap', 'application' %>
14
9
  </head>
15
10
 
@@ -1,15 +1,10 @@
1
1
  <!DOCTYPE html>
2
2
  <html lang="en">
3
3
  <head>
4
- <!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
5
- <!--[if lt IE 9]>
6
- <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
7
- <![endif]-->
8
4
  <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
9
5
  <meta name=viewport content='width=device-width,initial-scale=1' />
10
6
  <title><%%= @title.present? ? "#{@title} | Padrino Admin" : "#{pat(:custom_errors.title)} | Padrino Admin" %></title>
11
7
  <%%= favicon_tag "favicon.ico" %>
12
- <link href='http://fonts.googleapis.com/css?family=Varela' rel=stylesheet />
13
8
  <%%= stylesheet_link_tag 'bootstrap', 'application' %>
14
9
  </head>
15
10
 
@@ -1,10 +1,6 @@
1
1
  <!DOCTYPE html>
2
2
  <html lang="en">
3
3
  <head>
4
- <!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
5
- <!--[if lt IE 9]>
6
- <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
7
- <![endif]-->
8
4
  <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
9
5
  <meta name=viewport content='width=device-width,initial-scale=1' />
10
6
  <title><%%= @title.present? ? "#{@title} | Padrino Admin" : "#{pat('login.title')} | Padrino Admin" %></title>
@@ -52,8 +52,8 @@
52
52
  <%- @orm.columns.each_with_index do |column, i| -%>
53
53
  <%- if column.name == "created_at" || column.name == "updated_at" -%>
54
54
  <td class=list-column> <%%= time_ago_in_words <%= @orm.name_singular %>.<%= column.name %> %></td>
55
- <%- elsif column.type == :boolean -%>
56
- <td class=list-column> <%%= <%= @orm.name_singular %>.<%= column.name %> ? tag_icon(:ok) : tag_icon(:remove) %> </td>
55
+ <%- elsif @orm.field_type(column.type) == :check_box -%>
56
+ <td class=list-column> <%%= tag_icon(<%= @orm.name_singular %>.<%= column.name %> ? :check : :times) + content_tag(:span, <%= @orm.name_singular %>.<%= column.name %>.to_s, class: 'sr-only') unless <%= @orm.name_singular %>.<%= column.name %>.nil? %> </td>
57
57
  <%- else -%>
58
58
  <td class=list-column> <%%= <%= @orm.name_singular %>.<%= column.name %> %> </td>
59
59
  <%- end -%>
@@ -1,15 +1,10 @@
1
1
  !!! 5
2
2
  %html{lang: 'en'}
3
3
  %head
4
- <!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
5
- <!--[if lt IE 9]>
6
- <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
7
- <![endif]-->
8
4
  %meta{content: 'text/html; charset=utf-8', 'http-equiv' => 'Content-Type'}
9
5
  %meta{name: :viewport, content: 'width=device-width,initial-scale=1'}
10
6
  %title= @title.present? ? "#{@title} | Padrino Admin" : 'Padrino Admin'
11
7
  =favicon_tag 'favicon.ico'
12
- %link{href: 'http://fonts.googleapis.com/css?family=Varela', rel: :stylesheet}
13
8
  =stylesheet_link_tag 'bootstrap', 'application'
14
9
 
15
10
  %body
@@ -1,15 +1,10 @@
1
1
  !!! 5
2
2
  %html{lang: 'en'}
3
3
  %head
4
- <!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
5
- <!--[if lt IE 9]>
6
- <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
7
- <![endif]-->
8
4
  %meta{content: 'text/html; charset=utf-8', 'http-equiv' => 'Content-Type'}
9
5
  %meta{name: :viewport, content: 'width=device-width,initial-scale=1'}
10
6
  %title= @title.present? ? "#{@title} | Padrino Admin" : "#{pat(:custom_errors.title)} | Padrino Admin"
11
7
  =favicon_tag 'favicon.ico'
12
- %link{href: 'http://fonts.googleapis.com/css?family=Varela', rel: :stylesheet}
13
8
  =stylesheet_link_tag 'bootstrap', 'application'
14
9
 
15
10
  %body
@@ -1,10 +1,6 @@
1
1
  !!! 5
2
2
  %html{lang: 'en'}
3
3
  %head
4
- <!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
5
- <!--[if lt IE 9]>
6
- <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
7
- <![endif]-->
8
4
  %meta{content: 'text/html; charset=utf-8', 'http-equiv' => 'Content-Type'}
9
5
  %meta{name: :viewport, content: 'width=device-width,initial-scale=1'}
10
6
  %title= @title.present? ? "#{@title} | Padrino Admin" : "#{pat('login.title')} | Padrino Admin"
@@ -39,8 +39,8 @@
39
39
  <%- @orm.columns.each_with_index do |column, i| -%>
40
40
  <%- if column.name == "created_at" || column.name == "updated_at" -%>
41
41
  %td.list-column=time_ago_in_words <%= @orm.name_singular %>.<%= column.name %>
42
- <%- elsif column.type == :boolean -%>
43
- %td.list-column= <%= @orm.name_singular %>.<%= column.name %> ? tag_icon(:ok) : tag_icon(:remove)
42
+ <%- elsif @orm.field_type(column.type) == :check_box -%>
43
+ %td.list-column= tag_icon(<%= @orm.name_singular %>.<%= column.name %> ? :check : :times) + content_tag(:span, <%= @orm.name_singular %>.<%= column.name %>.to_s, class: 'sr-only') unless <%= @orm.name_singular %>.<%= column.name %>.nil?
44
44
  <%- else -%>
45
45
  %td.list-column=<%= @orm.name_singular %>.<%= column.name %>
46
46
  <%- end -%>
@@ -30,7 +30,6 @@
30
30
  if @<%= @orm.name_singular %>
31
31
  render '<%= @orm.name_plural %>/edit'
32
32
  else
33
- flash[:warning] = pat(:create_error, model: '<%= @orm.name_singular %>', id: params[:id])
34
33
  halt 404
35
34
  end
36
35
  end
@@ -49,7 +48,6 @@
49
48
  render '<%= @orm.name_plural %>/edit'
50
49
  end
51
50
  else
52
- flash[:warning] = pat(:update_warning, model: '<%= @orm.name_singular %>', id: params[:id])
53
51
  halt 404
54
52
  end
55
53
  end
@@ -65,7 +63,6 @@
65
63
  end
66
64
  redirect url(:<%= @orm.name_plural %>, :index)
67
65
  else
68
- flash[:warning] = pat(:delete_warning, model: '<%= @orm.name_singular %>', id: params[:id])
69
66
  halt 404
70
67
  end
71
68
  end
@@ -1,15 +1,10 @@
1
1
  doctype 5
2
2
  html lang='en'
3
3
  head
4
- <!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
5
- <!--[if lt IE 9]>
6
- <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
7
- <![endif]-->
8
4
  meta content='text/html; charset=utf-8' http-equiv='Content-Type'
9
5
  meta name='viewport' content='width=device-width,initial-scale=1'
10
6
  title = @title.present? ? "#{@title} | Padrino Admin" : "Padrino Admin"
11
7
  = favicon_tag 'favicon.ico'
12
- link href='http://fonts.googleapis.com/css?family=Varela' rel='stylesheet'
13
8
  = stylesheet_link_tag 'bootstrap', 'application'
14
9
 
15
10
  body
@@ -1,15 +1,10 @@
1
1
  doctype 5
2
2
  html lang="en" xmlns="http://www.w3.org/1999/xhtml"
3
3
  head
4
- <!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
5
- <!--[if lt IE 9]>
6
- <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
7
- <![endif]-->
8
4
  meta content='text/html; charset=utf-8' http-equiv='Content-Type'
9
5
  meta name='viewport' content='width=device-width,initial-scale=1'
10
6
  title = @title.present? ? "#{@title} | Padrino Admin" : "#{pat(:custom_errors.title)} | Padrino Admin"
11
7
  = favicon_tag 'favicon.ico'
12
- link href='http://fonts.googleapis.com/css?family=Varela' rel='stylesheet'
13
8
  = stylesheet_link_tag 'bootstrap', 'application'
14
9
 
15
10
  body
@@ -1,10 +1,6 @@
1
1
  doctype 5
2
2
  html lang="en" xmlns="http://www.w3.org/1999/xhtml"
3
3
  head
4
- <!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
5
- <!--[if lt IE 9]>
6
- <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
7
- <![endif]-->
8
4
  meta content='text/html; charset=utf-8' http-equiv='Content-Type'
9
5
  meta name='viewport' content='width=device-width,initial-scale=1'
10
6
  title = @title.present? ? "#{@title} | Padrino Admin" : "#{pat('login.title')} | Padrino Admin"
@@ -37,8 +37,8 @@ div class="tabs-content"
37
37
  <%- @orm.columns.each_with_index do |column, i| -%>
38
38
  <%- if column.name == 'created_at' || column.name == 'updated_at' -%>
39
39
  td class='list-column' = time_ago_in_words <%= @orm.name_singular %>.<%= column.name %>
40
- <%- elsif column.type == :boolean -%>
41
- td class='list-column' = <%= @orm.name_singular %>.<%= column.name %> ? tag_icon(:ok) : tag_icon(:remove)
40
+ <%- elsif @orm.field_type(column.type) == :check_box -%>
41
+ td class='list-column' = tag_icon(<%= @orm.name_singular %>.<%= column.name %> ? :check : :times) + content_tag(:span, <%= @orm.name_singular %>.<%= column.name %>.to_s, class: 'sr-only') unless <%= @orm.name_singular %>.<%= column.name %>.nil?
42
42
  <%- else -%>
43
43
  td class='list-column' = <%= @orm.name_singular %>.<%= column.name %>
44
44
  <%- end -%>
@@ -99,7 +99,9 @@ module Padrino
99
99
 
100
100
  def admin_model_obj
101
101
  @_admin_model_obj ||= Padrino::Inflections.constantize(settings.admin_model)
102
- rescue NameError
102
+ rescue NameError => e
103
+ raise unless Padrino::Admin.missing_constant?(e, settings.admin_model)
104
+
103
105
  raise Padrino::Admin::AccessControlError, "You must define an #{settings.admin_model} Model"
104
106
  end
105
107
  end
data/lib/padrino-admin.rb CHANGED
@@ -20,6 +20,26 @@ module Padrino
20
20
  I18n.load_path += Dir["#{__dir__}/padrino-admin/locale/**/*.yml"]
21
21
  end
22
22
  alias included registered
23
+
24
+ ##
25
+ # Whether +error+, raised looking up the constant +name+, says that constant (or one of its
26
+ # namespaces) is not defined, rather than coming from the code that defining it ran. The
27
+ # error must name a segment of +name+ and have been raised on that segment's parent, so a
28
+ # model whose body references another constant with the same short name isn't "missing".
29
+ #
30
+ def missing_constant?(error, name)
31
+ return false unless error.instance_of?(NameError)
32
+
33
+ segments = name.to_s.sub(/\A::/, '').split('::')
34
+ return false unless error.receiver.is_a?(::Module)
35
+
36
+ segments.each_index.any? do |index|
37
+ parent = index.zero? ? 'Object' : segments.first(index).join('::')
38
+ segments[index] == error.name.to_s && error.receiver.name == parent
39
+ end
40
+ rescue ArgumentError
41
+ false
42
+ end
23
43
  end
24
44
  end
25
45
  end
@@ -29,7 +49,7 @@ end
29
49
  #
30
50
  begin
31
51
  require 'padrino-gen'
32
- Padrino::Generators.load_paths << Dir["#{__dir__}/padrino-admin/generators/{actions,orm,admin_app,admin_page}.rb"]
52
+ Padrino::Generators.load_paths << Dir["#{__dir__}/padrino-admin/generators/{actions,keep_files,orm,admin_app,admin_page}.rb"]
33
53
  rescue LoadError
34
54
  # do nothing if padrino-gen is not available
35
55
  end
@@ -8,16 +8,21 @@ Gem::Specification.new do |s|
8
8
  s.authors = ['Padrino Team', 'Nathan Esquenazi', "Davide D'Agostino", 'Arthur Chiu']
9
9
  s.email = 'padrinorb@gmail.com'
10
10
  s.summary = 'Admin Dashboard for Padrino'
11
- s.homepage = 'http://www.padrinorb.com'
11
+ s.homepage = 'https://padrinorb.com'
12
12
  s.description = 'Admin View for Padrino applications'
13
+ s.required_ruby_version = '>= 2.7.8'
13
14
  s.required_rubygems_version = '>= 1.3.6'
14
15
  s.version = Padrino.version
15
- s.date = Time.now.strftime('%Y-%m-%d')
16
16
  s.license = 'MIT'
17
+ s.metadata = {
18
+ 'bug_tracker_uri' => 'https://github.com/padrino/padrino-framework/issues',
19
+ 'changelog_uri' => 'https://github.com/padrino/padrino-framework/blob/master/CHANGES.rdoc',
20
+ 'documentation_uri' => 'https://padrinorb.com/guides/',
21
+ 'source_code_uri' => 'https://github.com/padrino/padrino-framework'
22
+ }
17
23
 
18
24
  s.extra_rdoc_files = Dir['*.rdoc']
19
25
  s.files = `git ls-files`.split("\n")
20
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
26
  s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
22
27
  s.require_paths = ['lib']
23
28
  s.rdoc_options = ['--charset=UTF-8']