Skip to content

Commit

Permalink
Fix generator to take into account webpacker
Browse files Browse the repository at this point in the history
Generators were copying file when sprockets was not detected, even
if webpacker was present. This commit fixes the wrong behaviour.
tagliala committed Apr 26, 2020
1 parent 40f1aa6 commit 898e658
Showing 4 changed files with 30 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

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
@@ -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]
@@ -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'
@@ -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
@@ -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
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
31 changes: 14 additions & 17 deletions test/generators/cases/test_generators.rb
Original file line number Diff line number Diff line change
@@ -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
@@ -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

0 comments on commit 898e658

Please sign in to comment.