Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix generator to take into account webpacker #786

Merged
merged 1 commit into from
Apr 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* [FEATURE] Drop Ruby 2.3 support
* [FEATURE] Do not require `jquery-rails` gem ([#785](https://github.com/DavyJonesLocker/client_side_validations/pull/785))
* [FEATURE] Add support for many association validations ([#783](https://github.com/DavyJonesLocker/client_side_validations/pull/783))
* [BUGFIX] Fix Rails generators ([#786](https://github.com/DavyJonesLocker/client_side_validations/pull/786))

## 16.2.0 / 2020-04-10

Expand Down
21 changes: 14 additions & 7 deletions lib/generators/client_side_validations/copy_assets_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module ClientSideValidations
module Generators
class CopyAssetsGenerator < Rails::Generators::Base
def copy_javascript_asset
return unless self.class == CopyAssetsGenerator || !asset_pipeline_enabled?
return unless self.class == CopyAssetsGenerator || copy_assets?

assets.each do |asset|
source_paths << asset[:path]
Expand All @@ -13,7 +13,7 @@ def copy_javascript_asset
end

def self.asset_directory
if asset_pipeline_enabled?
if sprockets?
"app#{Rails.configuration.assets.prefix}/javascripts"
else
'public/javascripts'
Expand All @@ -28,9 +28,16 @@ def self.asset_file_names
assets.map { |asset| asset[:file] }.join(', ')
end

def self.asset_pipeline_enabled?
# Rails 4.1 doesn't provide :enabled in asset configuration, so we look for Sprockets
defined?(Sprockets).present?
def self.copy_assets?
!sprockets? && !webpacker?
end

def self.sprockets?
defined?(Sprockets)
end

def self.webpacker?
defined?(Webpacker)
end

def self.installation_message
Expand All @@ -49,8 +56,8 @@ def assets
CopyAssetsGenerator.assets
end

def asset_pipeline_enabled?
self.class.asset_pipeline_enabled?
def copy_assets?
self.class.copy_assets?
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'generators/client_side_validations/copy_assets_generator'
require_relative 'copy_assets_generator'

module ClientSideValidations
module Generators
Expand Down
31 changes: 14 additions & 17 deletions test/generators/cases/test_generators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,33 @@ class InstallGeneratorTest < Rails::Generators::TestCase
destination File.expand_path('../tmp', __dir__)
setup :prepare_destination

test 'Assert all files are properly created when no asset pipeline present' do
test 'Assert all files are properly created without sprockets and webpacker' do
stub_configuration
run_generator
assert_file 'config/initializers/client_side_validations.rb'
assert_file 'public/javascripts/rails.validations.js'
end

test 'Assert all files are properly created when asset pipeline present and disabled' do
test 'Assert all files are properly created with sprockets' do
stub_configuration
configuration = {}
configuration.stubs(:prefix).returns('/assets')
ClientSideValidations::Generators::CopyAssetsGenerator.stubs(:asset_pipeline_enabled?).returns false
ClientSideValidations::Generators::CopyAssetsGenerator.stubs(:sprockets?).returns true
Rails.configuration.stubs(:assets).returns(configuration)
run_generator
assert_file 'config/initializers/client_side_validations.rb'
assert_file 'public/javascripts/rails.validations.js'
assert_no_file 'public/javascripts/rails.validations.js'
end

test 'Assert all files are properly created when asset pipeline present and enabled' do
test 'Assert all files are properly created with webpacker' do
stub_configuration
configuration = {}
configuration.stubs(:prefix).returns('/assets')
ClientSideValidations::Generators::CopyAssetsGenerator.stubs(:asset_pipeline_enabled?).returns true
ClientSideValidations::Generators::CopyAssetsGenerator.stubs(:webpacker?).returns true
Rails.configuration.stubs(:assets).returns(configuration)
run_generator
assert_file 'config/initializers/client_side_validations.rb'
assert_no_file 'app/assets/javascripts/rails.validations.js'
assert_no_file 'public/javascripts/rails.validations.js'
end

def stub_configuration
Expand All @@ -49,30 +49,27 @@ class CopyAssetsGeneratorTest < Rails::Generators::TestCase
destination File.expand_path('../tmp', __dir__)
setup :prepare_destination

test 'Assert file is properly created when no asset pipeline present' do
test 'Assert file is properly created without sprockets and webpacker' do
stub_configuration
run_generator
assert_file 'public/javascripts/rails.validations.js'
end

test 'Assert file is properly created when asset pipeline present and disabled' do
test 'Assert file is properly created with sprockets' do
stub_configuration
configuration = {}
configuration.stubs(:prefix).returns('/assets')
ClientSideValidations::Generators::CopyAssetsGenerator.stubs(:asset_pipeline_enabled?).returns false
ClientSideValidations::Generators::CopyAssetsGenerator.stubs(:sprockets?).returns true
Rails.configuration.stubs(:assets).returns(configuration)
run_generator
assert_file 'public/javascripts/rails.validations.js'
assert_file 'app/assets/javascripts/rails.validations.js'
end

test 'Assert file is properly created when asset pipeline present and enabled' do
test 'Assert file is properly created with webpacker' do
stub_configuration
configuration = {}
configuration.stubs(:prefix).returns('/assets')
ClientSideValidations::Generators::CopyAssetsGenerator.stubs(:asset_pipeline_enabled?).returns true
Rails.configuration.stubs(:assets).returns(configuration)
ClientSideValidations::Generators::CopyAssetsGenerator.stubs(:webpacker?).returns true
run_generator
assert_file 'app/assets/javascripts/rails.validations.js'
assert_file 'public/javascripts/rails.validations.js'
end

def stub_configuration
Expand Down