godmin 0.9.9 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9db8432a519eafbc75e576858f5dc03c395d205f
4
- data.tar.gz: 14102d5be211e5ce235f02253d74b6d998720202
3
+ metadata.gz: 6dbff158f032a8e37f8d7d7dca27f42ad9ab6b6e
4
+ data.tar.gz: d9bbc151f8db24f624988ff92a96e623f749bcd7
5
5
  SHA512:
6
- metadata.gz: d921e069ef6d045e746c3fc144247712caf0a96fa91e7276c66c26b292274400e4dfeb4299fac5e5bb4aa9a36963453dcfb6fb5d0b0355a735d417b1b6077dba
7
- data.tar.gz: 3bf7c1fb9d173572327622e398d27185e5d1ad968c8667be9104379043fa1db594b1340ca7629629c644145f7fd125334daba01b91990f38dd5af29f406cb2c3
6
+ metadata.gz: de2cacda564c1f6dfde799848520ee3dacd25446c0bc6583b80c9b8214e606fd771fc63d9609026aeb6da5442c6b2fa1a3adacbded07a9a58581e5dca278038d
7
+ data.tar.gz: ac09d53249d52b5f2b7459889409f910316178dca90505102db6cb346b7eb6602a206d0b07d0096d874e38ff470c2b12107913bbf04f4a346c9ebd3268cc525f
data/.gitignore CHANGED
@@ -22,3 +22,4 @@ config/initializers/secret_token.rb
22
22
  *.swp
23
23
  /test/dummy/log/*
24
24
  Gemfile.lock
25
+ tags
@@ -1,7 +1,17 @@
1
- LineLength:
2
- Description: Limit lines to 120 characters.
1
+ AllCops:
2
+ RunRailsCops: true
3
+
4
+ Documentation:
5
+ Enabled: false
6
+
7
+ Metrics/LineLength:
3
8
  Max: 120
4
9
 
5
- StringLiterals:
6
- EnforcedStyle: double_quotes
10
+ Metrics/ClassLength:
11
+ Max: 300
7
12
 
13
+ Metrics/MethodLength:
14
+ Max: 25
15
+
16
+ Style/StringLiterals:
17
+ EnforcedStyle: double_quotes
@@ -1,5 +1,21 @@
1
1
  # Changelog
2
2
 
3
+ ### 0.10.0 - 2015-02-11
4
+ Features
5
+ - Shows the number of items in each scope in the scope tab (https://github.com/varvet/godmin/issues/16)
6
+ - Two new overridable methods for resources: `build_resource` and `find_resource`
7
+ - Translatable title (https://github.com/varvet/godmin/issues/17)
8
+
9
+ Bug fixes
10
+ - Fixes a bug where the wrong template would be picked (https://github.com/varvet/godmin/issues/39)
11
+ - Fixes a bug so the resolver works with namespaces templates.
12
+ - Fixes an autoloading issue (https://github.com/varvet/godmin/issues/60)
13
+ - Godmin rescues `NotAuthorizedError` and returns a 403 Forbidden HTTP status.
14
+
15
+ Other
16
+ - Cleaned up generators (https://github.com/varvet/godmin/issues/28)
17
+ - Restructured the locale files a bit
18
+
3
19
  ### 0.9.9 - 2015-01-23
4
20
  Features
5
21
  - Bump bootstrap to 3.3.3
data/README.md CHANGED
@@ -46,8 +46,9 @@ Add the gem to the application's `Gemfile`:
46
46
  gem "godmin"
47
47
  ```
48
48
 
49
- Run the install generator:
49
+ Bundle then run the install generator:
50
50
  ```sh
51
+ $ bundle install
51
52
  $ bin/rails generate godmin:install
52
53
  ```
53
54
 
@@ -76,8 +77,9 @@ Add the gem to the engine's gemspec, `admin/admin.gemspec`:
76
77
  s.add_dependency "godmin", "~> x.x.x"
77
78
  ```
78
79
 
79
- Run the install generator within the scope of the engine, i.e. note the leading `admin/`:
80
+ Bundle then run the install generator within the scope of the engine, i.e. note the leading `admin/`:
80
81
  ```sh
82
+ $ bundle install
81
83
  $ admin/bin/rails generate godmin:install
82
84
  ```
83
85
 
@@ -261,11 +263,13 @@ end
261
263
 
262
264
  Resources are made available to the views through instance variables. The index view can access the resources using `@resources` while show, new and edit can access the single resource using `@resource`.
263
265
 
264
- In order to modify what resources to fetch, there are three methods that can be overridden per resource controller:
266
+ In order to modify what resources to fetch, there are some methods that can be overridden per resource controller:
265
267
 
266
268
  - `resource_class`
267
- - `resource_relation`
269
+ - `resources_relation`
268
270
  - `resources`
271
+ - `build_resource`
272
+ - `find_resource`
269
273
 
270
274
  To change the class name of the resource from the default based on the controller name:
271
275
 
@@ -303,6 +307,32 @@ class ArticlesController
303
307
  end
304
308
  ```
305
309
 
310
+ To change the way a resource is constructed for `new` and `create` actions:
311
+
312
+ ```ruby
313
+ class ArticlesController
314
+ include Godmin::Resource
315
+
316
+ def build_resource(params)
317
+ article = resources_relation.new(params)
318
+ article.setup_more_things
319
+ article
320
+ end
321
+ end
322
+ ```
323
+
324
+ To change the way a resource is fetched for `show`, `edit`, `update` and `destroy` actions:
325
+
326
+ ```ruby
327
+ class ArticlesController
328
+ include Godmin::Resource
329
+
330
+ def find_resource(id)
331
+ resources_relation.find_by(slug: id)
332
+ end
333
+ end
334
+ ```
335
+
306
336
  ### Redirecting
307
337
 
308
338
  By default the user is redirected to the resource show page after create and update, and to the index page after destroy. To change this, there are four controller methods that can be overridden: `redirect_after_create`, `redirect_after_update`, `redirect_after_save`, and `redirect_after_destroy`.
@@ -552,18 +582,37 @@ end
552
582
 
553
583
  That is, everyone can list and view articles, only editors can create them, and only unpublished articles can be updated and destroyed.
554
584
 
585
+ When a user is not authorized to access a resource, `NotAuthorizedError` is raised. By default this error is rescued in Godmin and it renders some text and returns status code `403 Forbidden`.
586
+ If you want to change this behaviour you can rescue the error yourself in the appropriate `ApplicationController`.
587
+
588
+ ```ruby
589
+ module Admin
590
+ class ApplicationController < ActionController::Base
591
+ include Godmin::Application
592
+ include Godmin::Authentication
593
+ include Godmin::Authorization
594
+
595
+ # Renders 404 page and returns status code 404.
596
+ rescue_from NotAuthorizedError do
597
+ render file: "#{Rails.root}/public/404.html", status: 404, layout: false
598
+ end
599
+ end
600
+ end
601
+ ```
602
+
555
603
  ## Localization
556
604
 
557
605
  Godmin supports localization out of the box. Strings can be translated both globally and per resource, similar to how views work.
558
606
 
559
607
  For a list of translatable strings, [look here](https://github.com/varvet/godmin/blob/master/config/locales/en.yml).
560
608
 
561
- For instance, to translate the `godmin.batch_actions.select_all` string globally:
609
+ For instance, to translate the `godmin.batch_actions.buttons.select_all` string globally:
562
610
 
563
611
  ```yml
564
612
  godmin:
565
613
  batch_actions:
566
- select_all: {translation}
614
+ buttons:
615
+ select_all: {translation}
567
616
  ```
568
617
 
569
618
  Or, translate for a specific resource:
@@ -572,7 +621,8 @@ Or, translate for a specific resource:
572
621
  godmin:
573
622
  articles:
574
623
  batch_actions:
575
- select_all: {translation}
624
+ buttons:
625
+ select_all: {translation}
576
626
  ```
577
627
 
578
628
  In addition, all scopes, filters and batch actions that are added, can be localized:
@@ -581,14 +631,16 @@ In addition, all scopes, filters and batch actions that are added, can be locali
581
631
  godmin:
582
632
  articles:
583
633
  batch_actions:
584
- publish: {translation}
585
- unpublish: {translation}
634
+ labels:
635
+ publish: {translation}
636
+ unpublish: {translation}
586
637
  filters:
587
638
  labels:
588
639
  title: {translation}
589
640
  scopes:
590
- unpublished: {translation}
591
- published: {translation}
641
+ labels:
642
+ unpublished: {translation}
643
+ published: {translation}
592
644
  ```
593
645
 
594
646
  Godmin comes with built in support for English and Swedish.
@@ -661,6 +713,7 @@ Godmin.SelectBoxes.initializeSelectBox($el, {
661
713
  Some additional features are available as plugins:
662
714
 
663
715
  - [Godmin Uploads](https://github.com/varvet/godmin-uploads)
716
+ - [Godmin Tagger](https://github.com/varvet/godmin-tagger)
664
717
 
665
718
  ## Contributors
666
719
 
@@ -1,7 +1,7 @@
1
1
  <% if batch_action_map.present? %>
2
- <%= link_to translate_scoped("batch_actions.select_all"), "#", class: "btn btn-default",
2
+ <%= link_to translate_scoped("batch_actions.buttons.select_all"), "#", class: "btn btn-default",
3
3
  data: { behavior: "batch-actions-select batch-actions-select-all" } %>
4
- <%= link_to translate_scoped("batch_actions.deselect_all"), "#", class: "btn btn-default hidden",
4
+ <%= link_to translate_scoped("batch_actions.buttons.deselect_all"), "#", class: "btn btn-default hidden",
5
5
  data: { behavior: "batch-actions-select batch-actions-select-none" } %>
6
6
 
7
7
  <div class="btn-group">
@@ -3,7 +3,10 @@
3
3
  <ul class="nav nav-tabs">
4
4
  <% scope_map.each do |name, options| %>
5
5
  <li class="<%= "active" if params[:scope] == name.to_s %>">
6
- <%= link_to translate_scoped("scopes.#{name.to_s.underscore}", default: name.to_s.titleize), url_for(params.merge(scope: name, only_path: true).except(:page)) %>
6
+ <%= link_to url_for(params.merge(scope: name, only_path: true).except(:page)) do %>
7
+ <%= translate_scoped("scopes.labels.#{name.to_s.underscore}", default: name.to_s.titleize) %>
8
+ <span class="text-muted"> (<%= scope_count(name) %>)</span>
9
+ <% end %>
7
10
  </li>
8
11
  <% end %>
9
12
  </ul>
@@ -1,7 +1,7 @@
1
1
  <!doctype html>
2
2
  <html>
3
3
  <head>
4
- <title>Godmin</title>
4
+ <title><%= t("godmin.title") %></title>
5
5
  <%= stylesheet_link_tag [Godmin.namespace, "application"].compact.join("/"), media: "all" %>
6
6
  <%= javascript_include_tag [Godmin.namespace, "application"].compact.join("/") %>
7
7
  <%= csrf_meta_tags %>
@@ -2,7 +2,7 @@
2
2
  <nav class="navbar navbar-default navbar-static-top" role="navigation">
3
3
  <div class="container">
4
4
  <div class="navbar-header">
5
- <%= link_to "Godmin", root_path, class: "navbar-brand" %>
5
+ <%= link_to t("godmin.title"), root_path, class: "navbar-brand" %>
6
6
  </div>
7
7
  <div class="collapse navbar-collapse navbar-ex1-collapse">
8
8
  <%= render partial: "shared/navigation" %>
@@ -1,8 +1,10 @@
1
1
  en:
2
2
  godmin:
3
+ title: Godmin
3
4
  batch_actions:
4
- select_all: Select all
5
- deselect_all: Deselect all
5
+ buttons:
6
+ select_all: Select all
7
+ deselect_all: Deselect all
6
8
  confirm_message: Are you sure?
7
9
  filters:
8
10
  select:
@@ -1,8 +1,10 @@
1
1
  sv:
2
2
  godmin:
3
+ title: Godmin
3
4
  batch_actions:
4
- select_all: Markera alla
5
- deselect_all: Avmarkera alla
5
+ buttons:
6
+ select_all: Markera alla
7
+ deselect_all: Avmarkera alla
6
8
  confirm_message: Är du säker?
7
9
  filters:
8
10
  select:
@@ -1,15 +1,15 @@
1
- require "godmin/generators/base"
1
+ require "godmin/generators/named_base"
2
2
 
3
- class Godmin::AuthenticationGenerator < Godmin::Generators::Base
4
- argument :model, type: :string, default: "admin_user"
3
+ class Godmin::AuthenticationGenerator < Godmin::Generators::NamedBase
4
+ argument :name, type: :string, default: "admin_user"
5
5
 
6
6
  def create_model
7
- generate "model", "#{@model} email:string password_digest:text --no-test-framework"
7
+ generate "model", "#{name} email:string password_digest:text --no-test-framework"
8
8
  end
9
9
 
10
10
  def modify_model
11
- inject_into_file ["app/models", namespace, "#{@model.underscore}.rb"].compact.join("/"), after: "ActiveRecord::Base\n" do
12
- <<-END.strip_heredoc.indent(namespace.nil? ? 2 : 4)
11
+ inject_into_file File.join("app/models", class_path, "#{file_name}.rb"), after: "ActiveRecord::Base\n" do
12
+ <<-END.strip_heredoc.indent(namespace ? 4 : 2)
13
13
  include Godmin::Authentication::User
14
14
 
15
15
  def self.login_column
@@ -20,40 +20,20 @@ class Godmin::AuthenticationGenerator < Godmin::Generators::Base
20
20
  end
21
21
 
22
22
  def create_route
23
- inject_into_file "config/routes.rb", after: "godmin do\n" do
24
- <<-END.strip_heredoc.indent(4)
25
- resource :session, only: [:new, :create, :destroy]
26
- END
27
- end
23
+ route "resource :session, only: [:new, :create, :destroy]"
28
24
  end
29
25
 
30
26
  def create_sessions_controller
31
- create_file ["app/controllers", namespace, "sessions_controller.rb"].compact.join("/") do
32
- if namespace
33
- <<-END.strip_heredoc
34
- module #{namespace.camelize}
35
- class SessionsController < ApplicationController
36
- include Godmin::Authentication::Sessions
37
- end
38
- end
39
- END
40
- else
41
- <<-END.strip_heredoc
42
- class SessionsController < ApplicationController
43
- include Godmin::Authentication::Sessions
44
- end
45
- END
46
- end
47
- end
27
+ template "sessions_controller.rb", File.join("app/controllers", namespaced_path, "sessions_controller.rb")
48
28
  end
49
29
 
50
30
  def modify_application_controller
51
- inject_into_file ["app/controllers", namespace, "application_controller.rb"].compact.join("/"), after: "Godmin::Application\n" do
52
- <<-END.strip_heredoc.indent(namespace.nil? ? 2 : 4)
31
+ inject_into_file File.join("app/controllers", namespaced_path, "application_controller.rb"), after: "Godmin::Application\n" do
32
+ <<-END.strip_heredoc.indent(namespace ? 4 : 2)
53
33
  include Godmin::Authentication
54
34
 
55
35
  def admin_user_class
56
- #{[namespace, @model.underscore].compact.join("/").camelize}
36
+ #{class_name}
57
37
  end
58
38
  END
59
39
  end
@@ -0,0 +1,9 @@
1
+ <% if namespaced? -%>
2
+ require_dependency "<%= File.join(namespaced_path, "application_controller") %>"
3
+
4
+ <% end -%>
5
+ <% module_namespacing do -%>
6
+ class SessionsController < ApplicationController
7
+ include Godmin::Authentication::Sessions
8
+ end
9
+ <% end -%>
@@ -5,7 +5,7 @@ class Godmin::InstallGenerator < Godmin::Generators::Base
5
5
  create_file "config/initializers/godmin.rb" do
6
6
  <<-END.strip_heredoc
7
7
  Godmin.configure do |config|
8
- config.namespace = #{namespace ? "\"#{namespace}\"" : "nil"}
8
+ config.namespace = #{namespace ? "\"#{namespaced_path.join("/")}\"" : "nil"}
9
9
  end
10
10
  END
11
11
  end
@@ -21,29 +21,29 @@ class Godmin::InstallGenerator < Godmin::Generators::Base
21
21
  end
22
22
 
23
23
  def modify_application_controller
24
- inject_into_file ["app/controllers", namespace, "application_controller.rb"].compact.join("/"), after: "ActionController::Base\n" do
25
- <<-END.strip_heredoc.indent(namespace == nil ? 2 : 4)
24
+ inject_into_file File.join("app/controllers", namespaced_path, "application_controller.rb"), after: "ActionController::Base\n" do
25
+ <<-END.strip_heredoc.indent(namespace ? 4 : 2)
26
26
  include Godmin::Application
27
27
  END
28
28
  end
29
29
  end
30
30
 
31
31
  def modify_application_js
32
- inject_into_file ["app/assets/javascripts", namespace, "application.js"].compact.join("/"), before: "//= require_tree ." do
32
+ inject_into_file File.join("app/assets/javascripts", namespaced_path, "application.js"), before: "//= require_tree ." do
33
33
  "//= require godmin\n"
34
34
  end
35
35
  end
36
36
 
37
37
  def modify_application_css
38
- inject_into_file ["app/assets/stylesheets", namespace, "application.css"].compact.join("/"), before: " *= require_tree ." do
38
+ inject_into_file File.join("app/assets/stylesheets", namespaced_path, "application.css"), before: " *= require_tree ." do
39
39
  " *= require godmin\n"
40
40
  end
41
41
  end
42
42
 
43
43
  def require_library_if_namespaced
44
- return unless namespace
44
+ return unless namespaced?
45
45
 
46
- inject_into_file "lib/#{namespace}.rb", before: "require" do
46
+ inject_into_file File.join("lib", namespaced_path) + ".rb", before: "require" do
47
47
  <<-END.strip_heredoc
48
48
  require "godmin"
49
49
  END
@@ -1,29 +1,7 @@
1
- require "godmin/generators/base"
1
+ require "godmin/generators/named_base"
2
2
 
3
- class Godmin::PolicyGenerator < Godmin::Generators::Base
4
- argument :resource, type: :string
5
-
6
- def create_controller
7
- create_file ["app/policies", "#{policy_name}.rb"].compact.join("/") do
8
- if namespace
9
- <<-END.strip_heredoc
10
- module #{namespace.camelize}
11
- class #{@resource.underscore.camelize}Policy < Godmin::Authorization::Policy
12
- end
13
- end
14
- END
15
- else
16
- <<-END.strip_heredoc
17
- class #{@resource.underscore.camelize}Policy < Godmin::Authorization::Policy
18
- end
19
- END
20
- end
21
- end
22
- end
23
-
24
- private
25
-
26
- def policy_name
27
- [namespace, "#{@resource.underscore}_policy"].compact.join("/")
3
+ class Godmin::PolicyGenerator < Godmin::Generators::NamedBase
4
+ def create_policy
5
+ template "policy.rb", File.join("app/policies", class_path, "#{file_name}_policy.rb")
28
6
  end
29
7
  end
@@ -0,0 +1,23 @@
1
+ <% module_namespacing do -%>
2
+ class <%= class_name %>Policy < Godmin::Authorization::Policy
3
+ def index?
4
+ true
5
+ end
6
+
7
+ def show?
8
+ true
9
+ end
10
+
11
+ def create?
12
+ true
13
+ end
14
+
15
+ def update?
16
+ true
17
+ end
18
+
19
+ def destroy?
20
+ true
21
+ end
22
+ end
23
+ <% end -%>
@@ -1,56 +1,17 @@
1
- require "godmin/generators/base"
1
+ require "godmin/generators/named_base"
2
2
 
3
- class Godmin::ResourceGenerator < Godmin::Generators::Base
4
- argument :resource, type: :string
3
+ class Godmin::ResourceGenerator < Godmin::Generators::NamedBase
5
4
  argument :attributes, type: :array, default: [], banner: "attribute attribute"
6
5
 
7
- def create_route
6
+ def add_route
8
7
  inject_into_file "config/routes.rb", after: "godmin do\n" do
9
8
  <<-END.strip_heredoc.indent(4)
10
- resources :#{@resource.tableize}
9
+ resources :#{file_name.pluralize}
11
10
  END
12
11
  end
13
12
  end
14
13
 
15
14
  def create_controller
16
- create_file ["app/controllers", "#{controller_name}.rb"].compact.join("/") do
17
- if namespace
18
- <<-END.strip_heredoc
19
- module #{namespace.camelize}
20
- class #{@resource.tableize.camelize}Controller < ApplicationController
21
- include Godmin::Resource
22
-
23
- def attrs_for_index
24
- #{attributes.map(&:to_sym)}
25
- end
26
-
27
- def attrs_for_form
28
- #{attributes.map(&:to_sym)}
29
- end
30
- end
31
- end
32
- END
33
- else
34
- <<-END.strip_heredoc
35
- class #{@resource.tableize.camelize}Controller < ApplicationController
36
- include Godmin::Resource
37
-
38
- def attrs_for_index
39
- #{attributes.map(&:to_sym)}
40
- end
41
-
42
- def attrs_for_form
43
- #{attributes.map(&:to_sym)}
44
- end
45
- end
46
- END
47
- end
48
- end
49
- end
50
-
51
- private
52
-
53
- def controller_name
54
- [namespace, "#{@resource.tableize}_controller"].compact.join("/")
15
+ template "resource_controller.rb", File.join("app/controllers", class_path, "#{file_name.pluralize}_controller.rb")
55
16
  end
56
17
  end
@@ -0,0 +1,17 @@
1
+ <% if namespaced? -%>
2
+ require_dependency "<%= File.join(namespaced_path, "application_controller") %>"
3
+
4
+ <% end -%>
5
+ <% module_namespacing do -%>
6
+ class <%= class_name.pluralize %>Controller < ApplicationController
7
+ include Godmin::Resource
8
+
9
+ def attrs_for_index
10
+ <%= @attributes.map(&:to_sym) %>
11
+ end
12
+
13
+ def attrs_for_form
14
+ <%= @attributes.map(&:to_sym) %>
15
+ end
16
+ end
17
+ <% end -%>
@@ -16,7 +16,7 @@ module Godmin
16
16
  helper_method :authentication_enabled?
17
17
  helper_method :authorization_enabled?
18
18
 
19
- before_action :append_view_paths
19
+ before_action :prepend_view_paths
20
20
 
21
21
  layout "godmin/application"
22
22
  end
@@ -25,9 +25,9 @@ module Godmin
25
25
 
26
26
  private
27
27
 
28
- def append_view_paths
29
- append_view_path Godmin::EngineResolver.new(controller_name)
30
- append_view_path Godmin::GodminResolver.new(controller_name)
28
+ def prepend_view_paths
29
+ prepend_view_path Godmin::GodminResolver.new(controller_name)
30
+ prepend_view_path Godmin::EngineResolver.new(controller_name)
31
31
  end
32
32
 
33
33
  def authentication_enabled?
@@ -7,6 +7,10 @@ module Godmin
7
7
 
8
8
  included do
9
9
  helper_method :policy
10
+
11
+ rescue_from NotAuthorizedError do
12
+ render text: "You are not authorized to do this", status: 403, layout: "godmin/login"
13
+ end
10
14
  end
11
15
 
12
16
  def authorize(record)
@@ -3,10 +3,46 @@ require "active_support/all"
3
3
  module Godmin
4
4
  module Generators
5
5
  class Base < Rails::Generators::Base
6
+ def self.source_paths
7
+ %w(authentication install policy resource).map do |path|
8
+ File.expand_path("../../../generators/godmin/#{path}/templates", __FILE__)
9
+ end
10
+ end
11
+
6
12
  private
7
13
 
8
14
  def namespace
9
- Rails::Generators.namespace.to_s.underscore.presence
15
+ @namespace ||= Rails::Generators.namespace
16
+ end
17
+
18
+ def namespaced?
19
+ @namespaced ||= namespace.present?
20
+ end
21
+
22
+ def namespaced_path
23
+ @namespaced_path ||= begin
24
+ if namespaced?
25
+ namespace.name.classify.split("::").map(&:underscore)
26
+ else
27
+ []
28
+ end
29
+ end
30
+ end
31
+
32
+ def module_namespacing(&block)
33
+ content = capture(&block)
34
+ content = wrap_with_namespace(content) if namespaced?
35
+ concat(content)
36
+ end
37
+
38
+ def indent(content, multiplier = 2)
39
+ spaces = " " * multiplier
40
+ content.each_line.map { |line| line.blank? ? line : "#{spaces}#{line}" }.join
41
+ end
42
+
43
+ def wrap_with_namespace(content)
44
+ content = indent(content).chomp
45
+ "module #{namespace.name}\n#{content}\nend\n"
10
46
  end
11
47
  end
12
48
  end
@@ -0,0 +1,23 @@
1
+ require "godmin/generators/base"
2
+
3
+ module Godmin
4
+ module Generators
5
+ class NamedBase < Base
6
+ argument :name, type: :string
7
+
8
+ private
9
+
10
+ def class_name
11
+ @class_name ||= name.classify
12
+ end
13
+
14
+ def class_path
15
+ @class_path ||= namespaced_path + name.classify.deconstantize.split("::").map(&:underscore)
16
+ end
17
+
18
+ def file_name
19
+ @file_name ||= class_name.demodulize.underscore
20
+ end
21
+ end
22
+ end
23
+ end
@@ -2,16 +2,24 @@ module Godmin
2
2
  module Helpers
3
3
  module BatchActions
4
4
  def batch_action_link(name, options)
5
- if (options[:only].nil? && options[:except].nil?) ||
6
- (options[:only] && options[:only].include?(params[:scope].to_sym)) ||
7
- (options[:except] && !options[:except].include?(params[:scope].to_sym))
5
+ return unless include_batch_action_link?(options)
8
6
 
9
- link_to(translate_scoped("batch_actions.#{name}", default: name.to_s.titleize), "#", class: "btn btn-default hidden", data: {
7
+ link_to(
8
+ translate_scoped("batch_actions.labels.#{name}", default: name.to_s.titleize), "#",
9
+ class: "btn btn-default hidden", data: {
10
10
  behavior: "batch-actions-action-link",
11
11
  confirm: options[:confirm] ? translate_scoped("batch_actions.confirm_message") : false,
12
12
  value: name
13
- })
14
- end
13
+ }
14
+ )
15
+ end
16
+
17
+ private
18
+
19
+ def include_batch_action_link?(options)
20
+ (options[:only].nil? && options[:except].nil?) ||
21
+ (options[:only] && options[:only].include?(params[:scope].to_sym)) ||
22
+ (options[:except] && !options[:except].include?(params[:scope].to_sym))
15
23
  end
16
24
  end
17
25
  end
@@ -8,15 +8,14 @@ module Godmin
8
8
  template_paths(prefix, partial).each do |path|
9
9
  template = super(name, path, partial, details)
10
10
 
11
- if template.present?
12
- break
13
- end
11
+ break if template.present?
14
12
  end
15
13
 
16
14
  template
17
15
  end
18
16
 
19
17
  def template_paths(prefix, _partial)
18
+ prefix = clean_prefix(prefix)
20
19
  [
21
20
  [namespace, controller_name, prefix],
22
21
  [namespace, controller_name],
@@ -26,6 +25,12 @@ module Godmin
26
25
  [namespace]
27
26
  ].map { |path| path.compact.join("/") }.compact
28
27
  end
28
+
29
+ private
30
+
31
+ def clean_prefix(prefix)
32
+ prefix.gsub(/^#{namespace}\//, "")
33
+ end
29
34
  end
30
35
 
31
36
  class EngineResolver < Resolver
@@ -24,40 +24,12 @@ module Godmin
24
24
 
25
25
  before_action :set_resource_class
26
26
  before_action :set_resources, only: :index
27
- before_action :set_resource, only: [:show, :new, :edit, :update, :destroy]
27
+ before_action :set_resource, only: [:show, :new, :edit, :create, :update, :destroy]
28
28
 
29
29
  helper_method :attrs_for_index
30
30
  helper_method :attrs_for_form
31
31
  end
32
32
 
33
- def resource_class
34
- controller_name.classify.constantize
35
- end
36
-
37
- def resources_relation
38
- resource_class.all
39
- end
40
-
41
- def resources
42
- apply_pagination(
43
- apply_order(
44
- apply_filters(
45
- apply_scope(
46
- resources_relation
47
- )
48
- )
49
- )
50
- )
51
- end
52
-
53
- def resource
54
- if params[:id]
55
- resources_relation.find(params[:id])
56
- else
57
- resources_relation.new
58
- end
59
- end
60
-
61
33
  def index
62
34
  respond_to do |format|
63
35
  format.html
@@ -72,15 +44,11 @@ module Godmin
72
44
  end
73
45
  end
74
46
 
75
- def new
76
- end
47
+ def new; end
77
48
 
78
- def edit
79
- end
49
+ def edit; end
80
50
 
81
51
  def create
82
- @resource = resources_relation.new(resource_params)
83
-
84
52
  respond_to do |format|
85
53
  if @resource.save
86
54
  format.html { redirect_to redirect_after_create, notice: redirect_flash_message }
@@ -141,6 +109,51 @@ module Godmin
141
109
  authorize(@resource) if authorization_enabled?
142
110
  end
143
111
 
112
+ def resource_class
113
+ controller_name.classify.constantize
114
+ end
115
+
116
+ def resources_relation
117
+ resource_class.all
118
+ end
119
+
120
+ def resources
121
+ apply_pagination(
122
+ apply_order(
123
+ apply_filters(
124
+ apply_scope(
125
+ resources_relation
126
+ )
127
+ )
128
+ )
129
+ )
130
+ end
131
+
132
+ def resource
133
+ if params[:id]
134
+ find_resource(params[:id])
135
+ else
136
+ case action_name
137
+ when "create"
138
+ build_resource(resource_params)
139
+ when "new"
140
+ build_resource(nil)
141
+ end
142
+ end
143
+ end
144
+
145
+ def build_resource(params)
146
+ resources_relation.new(params)
147
+ end
148
+
149
+ def find_resource(id)
150
+ resources_relation.find(id)
151
+ end
152
+
153
+ def resource_params
154
+ params.require(resource_class.name.parameterize("_").to_sym).permit(attrs_for_form)
155
+ end
156
+
144
157
  def redirect_after_create
145
158
  redirect_after_save
146
159
  end
@@ -160,9 +173,5 @@ module Godmin
160
173
  def redirect_flash_message
161
174
  translate_scoped("flash.#{action_name}", resource: @resource.class.model_name.human)
162
175
  end
163
-
164
- def resource_params
165
- params.require(resource_class.name.underscore.to_sym).permit(attrs_for_form)
166
- end
167
176
  end
168
177
  end
@@ -31,30 +31,32 @@ module Godmin
31
31
  end
32
32
 
33
33
  def pages
34
- pages = (1..total_pages).to_a
34
+ @pages ||= begin
35
+ pages = (1..total_pages).to_a
35
36
 
36
- return pages unless total_pages > WINDOW_SIZE
37
+ return pages unless total_pages > WINDOW_SIZE
37
38
 
38
- if current_page < WINDOW_SIZE
39
- pages.slice(0, WINDOW_SIZE)
40
- elsif current_page > (total_pages - WINDOW_SIZE)
41
- pages.slice(-WINDOW_SIZE, WINDOW_SIZE)
42
- else
43
- pages.slice(pages.index(current_page) - (WINDOW_SIZE / 2), WINDOW_SIZE)
39
+ if current_page < WINDOW_SIZE
40
+ pages.slice(0, WINDOW_SIZE)
41
+ elsif current_page > (total_pages - WINDOW_SIZE)
42
+ pages.slice(-WINDOW_SIZE, WINDOW_SIZE)
43
+ else
44
+ pages.slice(pages.index(current_page) - (WINDOW_SIZE / 2), WINDOW_SIZE)
45
+ end
44
46
  end
45
47
  end
46
48
 
47
49
  def total_pages
48
- (total_resources.to_f / self.class.per_page).ceil
50
+ @total_pages ||= (total_resources.to_f / self.class.per_page).ceil
49
51
  end
50
52
 
51
53
  def total_resources
52
- resources.limit(nil).offset(nil).count
54
+ @total_resource ||= resources.limit(nil).offset(nil).count
53
55
  end
54
56
 
55
57
  module ClassMethods
56
58
  def per_page
57
- 10
59
+ 25
58
60
  end
59
61
  end
60
62
  end
@@ -5,16 +5,21 @@ module Godmin
5
5
 
6
6
  included do
7
7
  helper_method :scope_map
8
+ helper_method :scope_count
8
9
  end
9
10
 
10
11
  def scope_map
11
12
  self.class.scope_map
12
13
  end
13
14
 
15
+ def scope_count(scope)
16
+ apply_filters(
17
+ send("scope_#{scope}", resources_relation)
18
+ ).count
19
+ end
20
+
14
21
  def apply_scope(resources)
15
- if params[:scope].blank?
16
- params[:scope] = default_scope
17
- end
22
+ params[:scope] = default_scope if params[:scope].blank?
18
23
 
19
24
  if params[:scope] && scope_map.key?(params[:scope].to_sym)
20
25
  send("scope_#{params[:scope]}", resources)
@@ -1,3 +1,3 @@
1
1
  module Godmin
2
- VERSION = "0.9.9"
2
+ VERSION = "0.10.0"
3
3
  end
@@ -2,7 +2,7 @@ require "test_helper"
2
2
 
3
3
  module Godmin
4
4
  class ResolverTest < ActiveSupport::TestCase
5
- def test_foo_resolver_template_paths
5
+ def test_engine_resolver_template_paths
6
6
  namespaced_as "namespace" do
7
7
  assert_equal [
8
8
  "namespace/controller_name/prefix",
@@ -15,7 +15,20 @@ module Godmin
15
15
  end
16
16
  end
17
17
 
18
- def test_bar_resolver_template_paths
18
+ def test_engine_resolver_template_paths_when_namespace_is_in_prefix
19
+ namespaced_as "namespace" do
20
+ assert_equal [
21
+ "namespace/controller_name/prefix",
22
+ "namespace/controller_name",
23
+ "namespace/prefix",
24
+ "namespace/resource/prefix",
25
+ "namespace/resource",
26
+ "namespace"
27
+ ], EngineResolver.new("controller_name").template_paths("namespace/prefix", false)
28
+ end
29
+ end
30
+
31
+ def test_godmin_resolver_template_paths
19
32
  namespaced_as "namespace" do
20
33
  assert_equal [
21
34
  "godmin/controller_name/prefix",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: godmin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.9
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Ljungblad
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-01-23 00:00:00.000000000 Z
13
+ date: 2015-02-11 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bcrypt
@@ -194,9 +194,12 @@ files:
194
194
  - config/routes.rb
195
195
  - godmin.gemspec
196
196
  - lib/generators/godmin/authentication/authentication_generator.rb
197
+ - lib/generators/godmin/authentication/templates/sessions_controller.rb
197
198
  - lib/generators/godmin/install/install_generator.rb
198
199
  - lib/generators/godmin/policy/policy_generator.rb
200
+ - lib/generators/godmin/policy/templates/policy.rb
199
201
  - lib/generators/godmin/resource/resource_generator.rb
202
+ - lib/generators/godmin/resource/templates/resource_controller.rb
200
203
  - lib/godmin.rb
201
204
  - lib/godmin/application.rb
202
205
  - lib/godmin/authentication.rb
@@ -207,6 +210,7 @@ files:
207
210
  - lib/godmin/authorization/policy_finder.rb
208
211
  - lib/godmin/engine.rb
209
212
  - lib/godmin/generators/base.rb
213
+ - lib/godmin/generators/named_base.rb
210
214
  - lib/godmin/helpers/application.rb
211
215
  - lib/godmin/helpers/batch_actions.rb
212
216
  - lib/godmin/helpers/filters.rb