Skip to content

Commit

Permalink
Merge pull request #786 from DavyJonesLocker/feature/do-not-copy-asse…
Browse files Browse the repository at this point in the history
…ts-via-generator

Fix generator to take into account webpacker
  • Loading branch information
tagliala authored Apr 26, 2020
2 parents 40f1aa6 + 898e658 commit db59af7
Show file tree
Hide file tree
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
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

0 comments on commit db59af7

Please sign in to comment.