From 9a5005bca5752e9e52be8c7441ac5bc23526f84c Mon Sep 17 00:00:00 2001 From: bivanalhar Date: Thu, 6 Jun 2024 09:40:13 +0800 Subject: [PATCH 1/4] chore(spec): fix dependencies to support rails 6.1 - parent_name is deprecated in rails 6 and gone in 6.1 - I18n translation function has been updated - change assertion function in spec - bump ruby version to be at least 3.0 --- lib/rails_utils.rb | 4 +- rails_utils.gemspec | 11 ++- test/rails_utils_test.rb | 163 +++++++++++++++++++++++++-------------- 3 files changed, 113 insertions(+), 65 deletions(-) diff --git a/lib/rails_utils.rb b/lib/rails_utils.rb index e9351a1..dca88d1 100644 --- a/lib/rails_utils.rb +++ b/lib/rails_utils.rb @@ -27,12 +27,12 @@ def page_title(options={}) @page_title ||= begin default_page_title = "#{page_controller_class.capitalize} #{page_action_class.capitalize}" i18n_options = { default: default_page_title }.merge!(options) - I18n.t("#{page_controller_class}.#{page_action_class}.title", i18n_options) + I18n.t("#{page_controller_class}.#{page_action_class}.title", **i18n_options) end end def javascript_initialization(options = {}) - application_name = Rails.application.class.parent_name + application_name = Rails.application.class.module_parent_name js_namespace_name = page_controller_class_underscored js_function_name = page_action_class diff --git a/rails_utils.gemspec b/rails_utils.gemspec index 6c54fca..fc6f0fd 100644 --- a/rails_utils.gemspec +++ b/rails_utils.gemspec @@ -7,17 +7,20 @@ require "rails_utils/version" Gem::Specification.new do |s| s.name = "rails_utils" s.version = RailsUtils::VERSION - s.authors = ["Winston Teo"] - s.email = ["winston.yongwei+rails_utils@gmail.com"] - s.homepage = "https://github.com/winston/rails_utils" + s.authors = ["Bivan Alzacky Harmanto"] + s.email = ["bivan.alzacky@gmail.com"] + s.homepage = "https://github.com/Coursemology/rails_utils" s.summary = "Rails helpers based on opinionated project practices." s.description = "Rails helpers based on opinionated project practices. Currently useful for structuring CSS and JS." s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.md"] - s.add_dependency "rails", ">=3.2" + s.required_ruby_version = ">= 3.0" + + s.add_dependency "rails", "~> 6" s.add_development_dependency "minitest" , ">= 4.7.5" + s.add_development_dependency "sprockets", '~>3.0' s.add_development_dependency "mocha" s.license = 'MIT' diff --git a/test/rails_utils_test.rb b/test/rails_utils_test.rb index b0711bf..93ff40c 100644 --- a/test/rails_utils_test.rb +++ b/test/rails_utils_test.rb @@ -3,7 +3,7 @@ describe "RailsUtils::ActionViewExtensions" do let(:controller) { ActionController::Base.new } let(:request) { ActionDispatch::Request.new(flash: {}) } - let(:view) { ActionView::Base.new } + let(:view) { ActionView::Base.new({}, {}, "") } before do controller.request = request @@ -15,10 +15,10 @@ let(:controller_class) { "AnimeController" } let(:controller_name) { "anime" } - before { controller.stubs(:class).returns(controller_class) } - it "returns controller name" do - view.page_controller_class.must_equal controller_name + controller.stub :class, controller_class do + assert_equal(view.page_controller_class, controller_name) + end end end @@ -26,10 +26,10 @@ let(:controller_class) { "Super::Awesome::AnimeController" } let(:controller_name) { "super_awesome_anime" } - before { controller.stubs(:class).returns(controller_class) } - it "returns controller name" do - view.page_controller_class.must_equal controller_name + controller.stub :class, controller_class do + assert_equal(view.page_controller_class, controller_name) + end end end @@ -39,14 +39,21 @@ before do RailsUtils.configure do |config| + @rails_selector_format = config.selector_format config.selector_format = :hyphenated end + end - controller.stubs(:class).returns(controller_class) + after do + RailsUtils.configure do |config| + config.selector_format = @rails_selector_format + end end it "returns controller name" do - view.page_controller_class.must_equal controller_name + controller.stub :class, controller_class do + assert_equal(view.page_controller_class, controller_name) + end end end @@ -56,14 +63,14 @@ before do RailsUtils.configure do |config| - config.selector_format = "underscored" + config.selector_format = :underscored end - - controller.stubs(:class).returns(controller_class) end it "returns controller name" do - view.page_controller_class.must_equal controller_name + controller.stub :class, controller_class do + assert_equal(view.page_controller_class, controller_name) + end end end end @@ -81,10 +88,10 @@ [ "custom" , "custom" ], ].each do |action_name, expected| describe "when ##{action_name}" do - before { controller.stubs(:action_name).returns(action_name) } - it "returns #{expected}" do - view.page_action_class.must_equal expected + controller.stub :action_name, action_name do + assert_equal(view.page_action_class, expected) + end end end end @@ -94,39 +101,45 @@ let(:controller_name) { "anime" } let(:action_name) { "custom" } - before do - view.stubs(:page_controller_class).returns(controller_name) - view.stubs(:page_action_class).returns(action_name) - end - it "uses page_controller_class and page_action_class" do - view.page_class.must_equal "#{controller_name} #{action_name}" + view.stub :page_controller_class, controller_name do + view.stub :page_action_class, action_name do + assert_equal(view.page_class, "#{controller_name} #{action_name}") + end + end end end describe "#page_title" do let(:controller_name) { "anime" } - before do - view.stubs(:page_controller_class).returns(controller_name) - view.stubs(:page_action_class).returns(action_name) - end - describe 'when translation is missing' do let(:action_name) { "random" } let(:default_translation) { "#{controller_name.capitalize} #{action_name.capitalize}" } it "combines page_controller_class and page_action_class" do - view.page_title.must_equal default_translation + view.stub :page_controller_class, controller_name do + view.stub :page_action_class, action_name do + assert_equal(view.page_title, default_translation) + end + end end it "uses :default provided by gem user" do - view.page_title(default: 'my custom default').must_equal 'my custom default' + view.stub :page_controller_class, controller_name do + view.stub :page_action_class, action_name do + assert_equal(view.page_title(default: 'my custom default'), 'my custom default') + end + end end it "calling multiple times reuses first result (template renders before layout)" do - view.page_title(default: 'my custom default').must_equal 'my custom default' - view.page_title.must_equal 'my custom default' + view.stub :page_controller_class, controller_name do + view.stub :page_action_class, action_name do + assert_equal(view.page_title(default: 'my custom default'), 'my custom default') + assert_equal(view.page_title, 'my custom default') + end + end end end @@ -136,7 +149,11 @@ before { I18n.backend.store_translations("en", { controller_name.to_sym => { action_name.to_sym => { title: "An awesome title" } }}) } it "translates page title" do - view.page_title.must_equal "An awesome title" + view.stub :page_controller_class, controller_name do + view.stub :page_action_class, action_name do + assert_equal(view.page_title, "An awesome title") + end + end end end @@ -146,7 +163,11 @@ before { I18n.backend.store_translations("en", { controller_name.to_sym => { action_name.to_sym => { title: "An awesome title, %{name}" } }}) } it "translates page title" do - view.page_title(name: "bro").must_equal "An awesome title, bro" + view.stub :page_controller_class, controller_name do + view.stub :page_action_class, action_name do + assert_equal(view.page_title(name: "bro"), "An awesome title, bro") + end + end end end end @@ -155,21 +176,29 @@ let(:controller_class) { "Awesome::AnimeController" } let(:controller_name) { "awesome_anime" } - before do - controller.stubs(:class).returns(controller_class) - controller.stubs(:action_name).returns(action_name) - end + # before do + # controller.stubs(:class).returns(controller_class) + # controller.stubs(:action_name).returns(action_name) + # end describe "when controller name and action name are standard" do let(:action_name) { "custom" } it "invokes application" do - view.javascript_initialization.must_match "Dummy.init();" + controller.stub :class, controller_class do + controller.stub :action_name, action_name do + assert_match("Dummy.init();", view.javascript_initialization) + end + end end it "invokes controller and action javascript" do - view.javascript_initialization.must_match "Dummy.#{controller_name}.init();" - view.javascript_initialization.must_match "Dummy.#{controller_name}.#{action_name}.init();" + controller.stub :class, controller_class do + controller.stub :action_name, action_name do + assert_match("Dummy.#{controller_name}.init();", view.javascript_initialization) + assert_match("Dummy.#{controller_name}.#{action_name}.init();", view.javascript_initialization) + end + end end end @@ -177,7 +206,11 @@ let(:action_name) { "create" } it "replaces create with new" do - view.javascript_initialization.must_match "Dummy.#{controller_name}.new.init();" + controller.stub :class, controller_class do + controller.stub :action_name, action_name do + assert_match("Dummy.#{controller_name}.new.init();", view.javascript_initialization) + end + end end end @@ -185,7 +218,11 @@ let(:action_name) { "update" } it "replaces update with create" do - view.javascript_initialization.must_match "Dummy.#{controller_name}.edit.init();" + controller.stub :class, controller_class do + controller.stub :action_name, action_name do + assert_match("Dummy.#{controller_name}.edit.init();", view.javascript_initialization) + end + end end end @@ -193,8 +230,12 @@ let(:action_name) { "update" } it "uses the custom js init method" do - view.content_for(:js_init_method) { "custom" } - view.javascript_initialization.must_match "Dummy.#{controller_name}.custom.init();" + controller.stub :class, controller_class do + controller.stub :action_name, action_name do + view.content_for(:js_init_method) { "custom" } + assert_match("Dummy.#{controller_name}.custom.init();", view.javascript_initialization) + end + end end end @@ -202,7 +243,11 @@ let(:action_name) { "update" } it "does not generate an additional javascript method" do - view.javascript_initialization.wont_match "Dummy.#{controller_name}..init();" + controller.stub :class, controller_class do + controller.stub :action_name, action_name do + refute_includes("Dummy.#{controller_name}..init();", view.javascript_initialization) + end + end end end end @@ -231,11 +276,11 @@ def set_flash(key, message) before { set_flash key, expected_message } it "prints class '#{expected_class}'" do - view.flash_messages.must_match expected_class + assert_match(expected_class, view.flash_messages) end it "prints message '#{expected_message}'" do - view.flash_messages.must_match expected_message + assert_match(expected_message, view.flash_messages) end end end @@ -243,54 +288,54 @@ def set_flash(key, message) describe "when bootstrap is present" do it "can fade in and out" do set_flash :alert, "not important" - view.flash_messages.must_match(/fade in/) + assert_match(/fade in/, view.flash_messages) end it "can be dismissed" do set_flash :alert, "not important" - view.flash_messages.must_match(/data-dismiss=.*alert/) + assert_match(/data-dismiss=.*alert/, view.flash_messages) end end describe "options" do it "can allow override of button content (default 'x')" do set_flash :alert, "not important" - view.flash_messages.must_match %r{>x} - view.flash_messages(button_html: '').must_match %r{button type="button" class="close"} + assert_match(%r{>x}, view.flash_messages) + assert_match(%r{button type="button" class="close"}, view.flash_messages(button_html: '')) end it "can allow override of button css class (default 'close')" do set_flash :alert, "not important" - view.flash_messages.must_match %r{>x} - view.flash_messages(button_class: 'abc def').must_match %r{button type="button" class="abc def"} + assert_match(%r{>x}, view.flash_messages) + assert_match(%r{button type="button" class="abc def"}, view.flash_messages(button_class: 'abc def')) end end it "should strip " - view.flash_messages.wont_match " ``` @@ -196,7 +210,7 @@ Minitest-ed. To run all tests, just run `rake` or `rake test`. ## Author -Rails Utils is maintained by [Winston Teo](mailto:winstonyw+rails_utils@gmail.com). +Rails Utils is originally maintained by [Winston Teo](mailto:winstonyw+rails_utils@gmail.com). [You should follow Winston on Twitter](https://www.twitter.com/winstonyw), or find out more on [WinstonYW](http://www.winstonyw.com) and [LinkedIn](http://sg.linkedin.com/in/winstonyw). @@ -204,8 +218,5 @@ Rails Utils is maintained by [Winston Teo](mailto:winstonyw+rails_utils@gmail.co Copyright © 2013-2016 Winston Teo Yong Wei. Free software, released under the MIT license. - [version-badge]: https://badge.fury.io/rb/rails_utils.svg [rubygems]: https://rubygems.org/gems/rails_utils -[travis-badge]: https://travis-ci.org/winston/rails_utils.svg -[travis]: https://travis-ci.org/winston/rails_utils diff --git a/gemfiles/rails_6.0.gemfile b/gemfiles/rails_6.0.gemfile index 10c6284..e050d93 100644 --- a/gemfiles/rails_6.0.gemfile +++ b/gemfiles/rails_6.0.gemfile @@ -2,6 +2,6 @@ source "https://rubygems.org" -gem "rails", "~> 6.0" +gem "rails", "6.0.6.1" gemspec path: "../" diff --git a/gemfiles/rails_7.0.gemfile b/gemfiles/rails_7.0.gemfile index 9d2735b..af711ee 100644 --- a/gemfiles/rails_7.0.gemfile +++ b/gemfiles/rails_7.0.gemfile @@ -2,6 +2,6 @@ source "https://rubygems.org" -gem "rails", "~> 7.0" +gem "rails", "7.0.8.4" gemspec path: "../" diff --git a/lib/rails_utils/version.rb b/lib/rails_utils/version.rb index 9c04fbb..0de1fe9 100644 --- a/lib/rails_utils/version.rb +++ b/lib/rails_utils/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module RailsUtils - VERSION = "5.0.0" + VERSION = "6.0.0" end diff --git a/rails_utils.gemspec b/rails_utils.gemspec index e18faf9..dc2d011 100644 --- a/rails_utils.gemspec +++ b/rails_utils.gemspec @@ -20,8 +20,6 @@ Gem::Specification.new do |s| s.add_dependency "rails", ">= 6" s.add_development_dependency "minitest" , ">= 4.7.5" - s.add_development_dependency "sprockets-rails", '~>3.0' - s.add_development_dependency "sprockets", '~>3.0' s.add_development_dependency "appraisal", "~> 2.1" s.add_development_dependency "mocha" diff --git a/test/dummy/config/application.rb b/test/dummy/config/application.rb index dd719fe..71e68c6 100644 --- a/test/dummy/config/application.rb +++ b/test/dummy/config/application.rb @@ -1,10 +1,8 @@ require File.expand_path('../boot', __FILE__) # Pick the frameworks you want: -# require "active_record/railtie" require "action_controller/railtie" require "action_mailer/railtie" -require "sprockets/railtie" require "rails/test_unit/railtie" Bundler.require(*Rails.groups) @@ -55,10 +53,10 @@ class Application < Rails::Application # config.active_record.whitelist_attributes = true # Enable the asset pipeline - config.assets.enabled = true + # config.assets.enabled = true # Version of your assets, change this if you want to expire all your assets - config.assets.version = '1.0' + # config.assets.version = '1.0' end end diff --git a/test/rails_utils_test.rb b/test/rails_utils_test.rb index 93ff40c..9eaf191 100644 --- a/test/rails_utils_test.rb +++ b/test/rails_utils_test.rb @@ -3,7 +3,13 @@ describe "RailsUtils::ActionViewExtensions" do let(:controller) { ActionController::Base.new } let(:request) { ActionDispatch::Request.new(flash: {}) } - let(:view) { ActionView::Base.new({}, {}, "") } + let(:view) do + if Rails::VERSION::MAJOR == 6 && Rails::VERSION::MINOR == 0 + ActionView::Base.new + else + ActionView::Base.new({}, {}, "") + end + end before do controller.request = request