Skip to content

Commit db59af7

Browse files
authored
Merge pull request #786 from DavyJonesLocker/feature/do-not-copy-assets-via-generator
Fix generator to take into account webpacker
2 parents 40f1aa6 + 898e658 commit db59af7

File tree

4 files changed

+30
-25
lines changed

4 files changed

+30
-25
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* [FEATURE] Drop Ruby 2.3 support
66
* [FEATURE] Do not require `jquery-rails` gem ([#785](https://github.com/DavyJonesLocker/client_side_validations/pull/785))
77
* [FEATURE] Add support for many association validations ([#783](https://github.com/DavyJonesLocker/client_side_validations/pull/783))
8+
* [BUGFIX] Fix Rails generators ([#786](https://github.com/DavyJonesLocker/client_side_validations/pull/786))
89

910
## 16.2.0 / 2020-04-10
1011

lib/generators/client_side_validations/copy_assets_generator.rb

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module ClientSideValidations
44
module Generators
55
class CopyAssetsGenerator < Rails::Generators::Base
66
def copy_javascript_asset
7-
return unless self.class == CopyAssetsGenerator || !asset_pipeline_enabled?
7+
return unless self.class == CopyAssetsGenerator || copy_assets?
88

99
assets.each do |asset|
1010
source_paths << asset[:path]
@@ -13,7 +13,7 @@ def copy_javascript_asset
1313
end
1414

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

31-
def self.asset_pipeline_enabled?
32-
# Rails 4.1 doesn't provide :enabled in asset configuration, so we look for Sprockets
33-
defined?(Sprockets).present?
31+
def self.copy_assets?
32+
!sprockets? && !webpacker?
33+
end
34+
35+
def self.sprockets?
36+
defined?(Sprockets)
37+
end
38+
39+
def self.webpacker?
40+
defined?(Webpacker)
3441
end
3542

3643
def self.installation_message
@@ -49,8 +56,8 @@ def assets
4956
CopyAssetsGenerator.assets
5057
end
5158

52-
def asset_pipeline_enabled?
53-
self.class.asset_pipeline_enabled?
59+
def copy_assets?
60+
self.class.copy_assets?
5461
end
5562
end
5663
end

lib/generators/client_side_validations/install_generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
require 'generators/client_side_validations/copy_assets_generator'
3+
require_relative 'copy_assets_generator'
44

55
module ClientSideValidations
66
module Generators

test/generators/cases/test_generators.rb

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,33 @@ class InstallGeneratorTest < Rails::Generators::TestCase
1010
destination File.expand_path('../tmp', __dir__)
1111
setup :prepare_destination
1212

13-
test 'Assert all files are properly created when no asset pipeline present' do
13+
test 'Assert all files are properly created without sprockets and webpacker' do
1414
stub_configuration
1515
run_generator
1616
assert_file 'config/initializers/client_side_validations.rb'
1717
assert_file 'public/javascripts/rails.validations.js'
1818
end
1919

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

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

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

52-
test 'Assert file is properly created when no asset pipeline present' do
52+
test 'Assert file is properly created without sprockets and webpacker' do
5353
stub_configuration
5454
run_generator
5555
assert_file 'public/javascripts/rails.validations.js'
5656
end
5757

58-
test 'Assert file is properly created when asset pipeline present and disabled' do
58+
test 'Assert file is properly created with sprockets' do
5959
stub_configuration
6060
configuration = {}
6161
configuration.stubs(:prefix).returns('/assets')
62-
ClientSideValidations::Generators::CopyAssetsGenerator.stubs(:asset_pipeline_enabled?).returns false
62+
ClientSideValidations::Generators::CopyAssetsGenerator.stubs(:sprockets?).returns true
6363
Rails.configuration.stubs(:assets).returns(configuration)
6464
run_generator
65-
assert_file 'public/javascripts/rails.validations.js'
65+
assert_file 'app/assets/javascripts/rails.validations.js'
6666
end
6767

68-
test 'Assert file is properly created when asset pipeline present and enabled' do
68+
test 'Assert file is properly created with webpacker' do
6969
stub_configuration
70-
configuration = {}
71-
configuration.stubs(:prefix).returns('/assets')
72-
ClientSideValidations::Generators::CopyAssetsGenerator.stubs(:asset_pipeline_enabled?).returns true
73-
Rails.configuration.stubs(:assets).returns(configuration)
70+
ClientSideValidations::Generators::CopyAssetsGenerator.stubs(:webpacker?).returns true
7471
run_generator
75-
assert_file 'app/assets/javascripts/rails.validations.js'
72+
assert_file 'public/javascripts/rails.validations.js'
7673
end
7774

7875
def stub_configuration

0 commit comments

Comments
 (0)