diff --git a/.gitignore b/.gitignore index 46e7ea0f..1dd526e8 100644 --- a/.gitignore +++ b/.gitignore @@ -23,6 +23,7 @@ config/initializers/secret_token.rb /test/dummy/log/* /test/dummy/tmp/* /test/dummy/db/*.sqlite3 +/test/tmp/* Gemfile.lock .tags gemfiles/.bundle diff --git a/lib/tasks/godmin_tasks.rake b/lib/tasks/godmin_tasks.rake deleted file mode 100644 index 28079c10..00000000 --- a/lib/tasks/godmin_tasks.rake +++ /dev/null @@ -1,4 +0,0 @@ -# desc "Explaining what the task does" -# task :godmin do -# # Task goes here -# end diff --git a/test/fakes/article.rb b/test/fakes/article.rb index c95f3bbb..a6b023d6 100644 --- a/test/fakes/article.rb +++ b/test/fakes/article.rb @@ -5,7 +5,7 @@ def self.table_name end def self.column_names - ["id", "title"] + %w[id title] end end end diff --git a/test/fakes/article_service.rb b/test/fakes/article_service.rb index 5f98b7eb..e596552a 100644 --- a/test/fakes/article_service.rb +++ b/test/fakes/article_service.rb @@ -13,8 +13,8 @@ class ArticleService scope :published filter :title - filter :country, as: :select, collection: %w(Sweden Canada) - filter :tags, as: :multiselect, collection: %w(Apple Banana) + filter :country, as: :select, collection: %w[Sweden Canada] + filter :tags, as: :multiselect, collection: %w[Apple Banana] batch_action :unpublish batch_action :publish, confirm: true, only: [:unpublished], except: [:published] @@ -25,7 +25,7 @@ def initialize(*) end def resources_relation - [:foo, :bar, :baz] + %i[foo bar baz] end def order_by_foobar(resources, direction) diff --git a/test/generators/resource_generator_test.rb b/test/generators/resource_generator_test.rb new file mode 100644 index 00000000..7ec9b61f --- /dev/null +++ b/test/generators/resource_generator_test.rb @@ -0,0 +1,77 @@ +require "test_helper" +require "generators/godmin/resource/resource_generator" + +module Godmin + class ResourceGeneratorTest < ::Rails::Generators::TestCase + tests ResourceGenerator + destination File.expand_path("../../tmp", __FILE__) + setup :prepare_destination + + def test_resource_generator_in_standalone_install + system "cd #{destination_root} && rails new . --skip-test --skip-spring --skip-bundle --skip-git --quiet" + system "cd #{destination_root} && bin/rails generate godmin:install --quiet" + system "cd #{destination_root} && bin/rails generate godmin:resource foo bar --quiet" + + assert_file "config/routes.rb", /resources :foos/ + assert_file "app/views/shared/_navigation.html.erb", /<%= navbar_item Foo %>/ + + assert_file "app/controllers/foos_controller.rb" do |content| + expected_content = <<-CONTENT.strip_heredoc + class FoosController < ApplicationController + include Godmin::Resources::ResourceController + end + CONTENT + assert_match expected_content, content + end + + assert_file "app/services/foo_service.rb" do |content| + expected_content = <<-CONTENT.strip_heredoc + class FooService + include Godmin::Resources::ResourceService + + attrs_for_index :bar + attrs_for_show :bar + attrs_for_form :bar + end + CONTENT + assert_match expected_content, content + end + end + + def test_resource_generator_in_engine_install + system "cd #{destination_root} && rails new . --skip-test --skip-spring --skip-bundle --skip-git --quiet" + system "cd #{destination_root} && bin/rails plugin new fakemin --mountable --quiet" + system "cd #{destination_root} && fakemin/bin/rails generate godmin:install --quiet" + system "cd #{destination_root} && fakemin/bin/rails generate godmin:resource foo bar --quiet" + + assert_file "fakemin/config/routes.rb", /resources :foos/ + assert_file "fakemin/app/views/fakemin/shared/_navigation.html.erb", /<%= navbar_item Foo %>/ + + assert_file "fakemin/app/controllers/fakemin/foos_controller.rb" do |content| + expected_content = <<-CONTENT.strip_heredoc + module Fakemin + class FoosController < ApplicationController + include Godmin::Resources::ResourceController + end + end + CONTENT + assert_match expected_content, content + end + + assert_file "fakemin/app/services/fakemin/foo_service.rb" do |content| + expected_content = <<-CONTENT.strip_heredoc + module Fakemin + class FooService + include Godmin::Resources::ResourceService + + attrs_for_index :bar + attrs_for_show :bar + attrs_for_form :bar + end + end + CONTENT + assert_match expected_content, content + end + end + end +end diff --git a/test/unit/authorization/policy_finder_test.rb b/test/unit/authorization/policy_finder_test.rb new file mode 100644 index 00000000..d97afcb2 --- /dev/null +++ b/test/unit/authorization/policy_finder_test.rb @@ -0,0 +1,55 @@ +require "test_helper" + +module Godmin + module Authorization + class PolicyFinderTest < ActiveSupport::TestCase + class Article; end + class ArticlePolicy; end + + def test_find_by_model + klass = Class.new do + extend ActiveModel::Naming + + def self.name + "Article" + end + end + + policy = PolicyFinder.find(klass, Godmin::Authorization::PolicyFinderTest) + assert_equal ArticlePolicy, policy + end + + def test_find_by_class + policy = PolicyFinder.find(Article) + assert_equal ArticlePolicy, policy + end + + def test_find_by_symbol + policy = PolicyFinder.find(:article, Godmin::Authorization::PolicyFinderTest) + assert_equal ArticlePolicy, policy + end + + def test_override_policy_class_on_class + klass = Class.new do + def self.policy_class + ArticlePolicy + end + end + + policy = PolicyFinder.find(klass) + assert_equal ArticlePolicy, policy + end + + def test_override_policy_class_on_instance + klass = Class.new do + def policy_class + ArticlePolicy + end + end + + policy = PolicyFinder.find(klass.new) + assert_equal ArticlePolicy, policy + end + end + end +end diff --git a/test/lib/godmin/engine_wrapper_test.rb b/test/unit/engine_wrapper_test.rb similarity index 100% rename from test/lib/godmin/engine_wrapper_test.rb rename to test/unit/engine_wrapper_test.rb diff --git a/test/lib/godmin/helpers/filters_test.rb b/test/unit/helpers/filters_test.rb similarity index 100% rename from test/lib/godmin/helpers/filters_test.rb rename to test/unit/helpers/filters_test.rb diff --git a/test/lib/godmin/paginator_test.rb b/test/unit/paginator_test.rb similarity index 100% rename from test/lib/godmin/paginator_test.rb rename to test/unit/paginator_test.rb diff --git a/test/lib/godmin/resolver_test.rb b/test/unit/resolver_test.rb similarity index 100% rename from test/lib/godmin/resolver_test.rb rename to test/unit/resolver_test.rb diff --git a/test/lib/godmin/resources/resource_service/batch_actions_test.rb b/test/unit/resources/resource_service/batch_actions_test.rb similarity index 100% rename from test/lib/godmin/resources/resource_service/batch_actions_test.rb rename to test/unit/resources/resource_service/batch_actions_test.rb diff --git a/test/lib/godmin/resources/resource_service/filters_test.rb b/test/unit/resources/resource_service/filters_test.rb similarity index 100% rename from test/lib/godmin/resources/resource_service/filters_test.rb rename to test/unit/resources/resource_service/filters_test.rb diff --git a/test/lib/godmin/resources/resource_service/ordering_test.rb b/test/unit/resources/resource_service/ordering_test.rb similarity index 100% rename from test/lib/godmin/resources/resource_service/ordering_test.rb rename to test/unit/resources/resource_service/ordering_test.rb diff --git a/test/lib/godmin/resources/resource_service/pagination_test.rb b/test/unit/resources/resource_service/pagination_test.rb similarity index 100% rename from test/lib/godmin/resources/resource_service/pagination_test.rb rename to test/unit/resources/resource_service/pagination_test.rb diff --git a/test/lib/godmin/resources/resource_service/scopes_test.rb b/test/unit/resources/resource_service/scopes_test.rb similarity index 100% rename from test/lib/godmin/resources/resource_service/scopes_test.rb rename to test/unit/resources/resource_service/scopes_test.rb diff --git a/test/lib/godmin/resources/resource_service_test.rb b/test/unit/resources/resource_service_test.rb similarity index 100% rename from test/lib/godmin/resources/resource_service_test.rb rename to test/unit/resources/resource_service_test.rb