ijs-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 78b4a993a974212cdf99c5cc8bcd4a43523b01fcc9a8095a4587cdef34f9d134
4
+ data.tar.gz: c4901a93c48ebb118f6880335b9a976e50736f745fe541759f3303f086fee802
5
+ SHA512:
6
+ metadata.gz: 6ab97488d8ecb9a9f6424383f4370a9300dbfb2e405094cd16c7a529a4ad3c8b2bb7c368e2d3eec57d2f6f5697beb2f5a62fea427301e6d550f6957ee32a1695
7
+ data.tar.gz: c71f8301f4ab085f0c2590acba06052a3d390b1339f4e3b25a268d875216ffa3e782f01e8f4fbb301dbef77c57a87b9ef9794fb0427a9e5bcd205fc98c393742
data/.gitignore ADDED
@@ -0,0 +1,46 @@
1
+ ##
2
+ # OS Files
3
+ ##
4
+
5
+ .DS_Store
6
+ .Trashes
7
+ ehthumbs.db
8
+ Thumbs.db
9
+
10
+ ##
11
+ # Databases
12
+ ##
13
+
14
+ *.sqlite
15
+
16
+ ##
17
+ # Logs
18
+ ##
19
+
20
+ *.log
21
+
22
+ ##
23
+ # Packages
24
+ ##
25
+
26
+ *.7z
27
+ *.dmg
28
+ *.gz
29
+ *.iso
30
+ *.jar
31
+ *.rar
32
+ *.tar
33
+ *.zip
34
+
35
+ ##
36
+ # Ruby Files
37
+ ##
38
+
39
+ /.bundle/
40
+ /.yardoc
41
+ /_yardoc/
42
+ /pkg/
43
+ /spec/internal/log/
44
+ /spec/internal/tmp/
45
+ /tmp/
46
+ /travis/gemfiles/*.gemfile.lock
data/.rbenv-gemsets ADDED
@@ -0,0 +1 @@
1
+ ijs-rails
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require rails_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,25 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'bin/**/*'
4
+
5
+ Metrics/AbcSize:
6
+ Max: 30
7
+
8
+ Metrics/LineLength:
9
+ Max: 120
10
+
11
+ Metrics/BlockLength:
12
+ Enabled: false
13
+
14
+ Metrics/ClassLength:
15
+ Enabled: false
16
+
17
+ Metrics/MethodLength:
18
+ Enabled: false
19
+
20
+ Naming/FileName:
21
+ Exclude:
22
+ - 'lib/ijs-rails.rb'
23
+
24
+ Style/Documentation:
25
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.6.5
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.6.5
4
+ gemfile:
5
+ - travis/gemfiles/6.0.gemfile
6
+ before_install:
7
+ - gem update --system
8
+ - gem install bundler
9
+ env:
10
+ global:
11
+ - RSPEC_DEFAULT_FORMATTER=doc
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,169 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ijs-rails (0.1.0)
5
+ activerecord (>= 3.0.0)
6
+ activesupport (>= 3.0.0)
7
+ uglifier (>= 2.3)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ actionpack (6.0.2.1)
13
+ actionview (= 6.0.2.1)
14
+ activesupport (= 6.0.2.1)
15
+ rack (~> 2.0, >= 2.0.8)
16
+ rack-test (>= 0.6.3)
17
+ rails-dom-testing (~> 2.0)
18
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
19
+ actionview (6.0.2.1)
20
+ activesupport (= 6.0.2.1)
21
+ builder (~> 3.1)
22
+ erubi (~> 1.4)
23
+ rails-dom-testing (~> 2.0)
24
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
25
+ activemodel (6.0.2.1)
26
+ activesupport (= 6.0.2.1)
27
+ activerecord (6.0.2.1)
28
+ activemodel (= 6.0.2.1)
29
+ activesupport (= 6.0.2.1)
30
+ activesupport (6.0.2.1)
31
+ concurrent-ruby (~> 1.0, >= 1.0.2)
32
+ i18n (>= 0.7, < 2)
33
+ minitest (~> 5.1)
34
+ tzinfo (~> 1.1)
35
+ zeitwerk (~> 2.2)
36
+ ast (2.4.0)
37
+ builder (3.2.4)
38
+ coderay (1.1.2)
39
+ combustion (1.1.2)
40
+ activesupport (>= 3.0.0)
41
+ railties (>= 3.0.0)
42
+ thor (>= 0.14.6)
43
+ concurrent-ruby (1.1.6)
44
+ crass (1.0.6)
45
+ diff-lcs (1.3)
46
+ erubi (1.9.0)
47
+ execjs (2.7.0)
48
+ ffi (1.12.2)
49
+ formatador (0.2.5)
50
+ guard (2.16.1)
51
+ formatador (>= 0.2.4)
52
+ listen (>= 2.7, < 4.0)
53
+ lumberjack (>= 1.0.12, < 2.0)
54
+ nenv (~> 0.1)
55
+ notiffany (~> 0.0)
56
+ pry (>= 0.9.12)
57
+ shellany (~> 0.0)
58
+ thor (>= 0.18.1)
59
+ guard-compat (1.2.1)
60
+ guard-rspec (4.7.3)
61
+ guard (~> 2.1)
62
+ guard-compat (~> 1.1)
63
+ rspec (>= 2.99.0, < 4.0)
64
+ i18n (1.8.2)
65
+ concurrent-ruby (~> 1.0)
66
+ jaro_winkler (1.5.4)
67
+ listen (3.2.1)
68
+ rb-fsevent (~> 0.10, >= 0.10.3)
69
+ rb-inotify (~> 0.9, >= 0.9.10)
70
+ loofah (2.4.0)
71
+ crass (~> 1.0.2)
72
+ nokogiri (>= 1.5.9)
73
+ lumberjack (1.2.4)
74
+ method_source (0.9.2)
75
+ mini_portile2 (2.4.0)
76
+ minitest (5.14.0)
77
+ nenv (0.3.0)
78
+ nokogiri (1.10.8)
79
+ mini_portile2 (~> 2.4.0)
80
+ notiffany (0.1.3)
81
+ nenv (~> 0.1)
82
+ shellany (~> 0.0)
83
+ parallel (1.19.1)
84
+ parser (2.7.0.2)
85
+ ast (~> 2.4.0)
86
+ pry (0.12.2)
87
+ coderay (~> 1.1.0)
88
+ method_source (~> 0.9.0)
89
+ rack (2.2.2)
90
+ rack-test (1.1.0)
91
+ rack (>= 1.0, < 3)
92
+ rails-dom-testing (2.0.3)
93
+ activesupport (>= 4.2.0)
94
+ nokogiri (>= 1.6)
95
+ rails-html-sanitizer (1.3.0)
96
+ loofah (~> 2.3)
97
+ railties (6.0.2.1)
98
+ actionpack (= 6.0.2.1)
99
+ activesupport (= 6.0.2.1)
100
+ method_source
101
+ rake (>= 0.8.7)
102
+ thor (>= 0.20.3, < 2.0)
103
+ rainbow (3.0.0)
104
+ rake (13.0.1)
105
+ rb-fsevent (0.10.3)
106
+ rb-inotify (0.10.1)
107
+ ffi (~> 1.0)
108
+ rexml (3.2.4)
109
+ rspec (3.9.0)
110
+ rspec-core (~> 3.9.0)
111
+ rspec-expectations (~> 3.9.0)
112
+ rspec-mocks (~> 3.9.0)
113
+ rspec-core (3.9.1)
114
+ rspec-support (~> 3.9.1)
115
+ rspec-expectations (3.9.0)
116
+ diff-lcs (>= 1.2.0, < 2.0)
117
+ rspec-support (~> 3.9.0)
118
+ rspec-html-matchers (0.9.2)
119
+ nokogiri (~> 1)
120
+ rspec (>= 3.0.0.a, < 4)
121
+ rspec-mocks (3.9.1)
122
+ diff-lcs (>= 1.2.0, < 2.0)
123
+ rspec-support (~> 3.9.0)
124
+ rspec-rails (3.9.0)
125
+ actionpack (>= 3.0)
126
+ activesupport (>= 3.0)
127
+ railties (>= 3.0)
128
+ rspec-core (~> 3.9.0)
129
+ rspec-expectations (~> 3.9.0)
130
+ rspec-mocks (~> 3.9.0)
131
+ rspec-support (~> 3.9.0)
132
+ rspec-support (3.9.2)
133
+ rubocop (0.80.0)
134
+ jaro_winkler (~> 1.5.1)
135
+ parallel (~> 1.10)
136
+ parser (>= 2.7.0.1)
137
+ rainbow (>= 2.2.2, < 4.0)
138
+ rexml
139
+ ruby-progressbar (~> 1.7)
140
+ unicode-display_width (>= 1.4.0, < 1.7)
141
+ ruby-progressbar (1.10.1)
142
+ shellany (0.0.1)
143
+ thor (1.0.1)
144
+ thread_safe (0.3.6)
145
+ tzinfo (1.2.6)
146
+ thread_safe (~> 0.1)
147
+ uglifier (4.2.0)
148
+ execjs (>= 0.3.0, < 3)
149
+ unicode-display_width (1.6.1)
150
+ yard (0.9.24)
151
+ zeitwerk (2.2.2)
152
+
153
+ PLATFORMS
154
+ ruby
155
+
156
+ DEPENDENCIES
157
+ bundler (~> 2.1)
158
+ combustion (~> 1.1)
159
+ guard-rspec (~> 4.7)
160
+ ijs-rails!
161
+ rake (~> 13.0)
162
+ rspec (~> 3.9)
163
+ rspec-html-matchers (~> 0.9.2)
164
+ rspec-rails (~> 3.9)
165
+ rubocop (~> 0.80.0)
166
+ yard (~> 0.9.24)
167
+
168
+ BUNDLED WITH
169
+ 2.1.4
data/Guardfile ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ guard :rspec, cmd: 'bundle exec rspec' do
4
+ require 'guard/rspec/dsl'
5
+
6
+ dsl = Guard::RSpec::Dsl.new(self)
7
+
8
+ # RSpec files
9
+ rspec = dsl.rspec
10
+ watch(rspec.spec_helper) { rspec.spec_dir }
11
+ watch(rspec.spec_support) { rspec.spec_dir }
12
+ watch(rspec.spec_files)
13
+
14
+ # Ruby files
15
+ ruby = dsl.ruby
16
+ dsl.watch_spec_files_for(ruby.lib_files)
17
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Nialto Services
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Inline JS for Rails
2
+
3
+ [![Build Status](https://travis-ci.org/NialtoServices/ijs-rails.svg?branch=master)](https://travis-ci.org/NialtoServices/ijs-rails)
4
+
5
+ Inline JS can be used to include JavaScript files in your views in a minified format.
6
+
7
+ ## Installation
8
+
9
+ You can install **Inline JS for Rails** using the following command:
10
+
11
+ $ gem install ijs-rails
12
+
13
+ Or, by adding the following to your `Gemfile`:
14
+
15
+ ```ruby
16
+ gem 'ijs-rails', '~> 0.1'
17
+ ```
18
+
19
+ ### Usage
20
+
21
+ Let's start with an example script *hello.js* which should be saved in the *app/javascript/inline* directory.
22
+
23
+ ```js
24
+ //
25
+ // Example Script
26
+ //
27
+
28
+ alert('Hello, World!');
29
+ ```
30
+
31
+ Next in a view, use the *render_ijs* method to render a *<script>* tag containing the minified script:
32
+
33
+ ```ruby
34
+ <%= render_ijs 'hello' %>
35
+ ```
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bundle exec rake spec` to run the tests.
40
+
41
+ To install this gem onto your machine, run `bundle exec rake install`.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/config.ru ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+
6
+ Bundler.require :default, :development
7
+
8
+ Combustion.initialize! :all
9
+ run Combustion::Application
data/ijs-rails.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require 'ijs-rails/constants'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'ijs-rails'
10
+ spec.version = IJSRails::VERSION
11
+ spec.authors = ['Nialto Services']
12
+ spec.email = ['support@nialtoservices.co.uk']
13
+
14
+ spec.summary = 'Inline JavaScript helper for Ruby on Rails.'
15
+ spec.homepage = 'https://github.com/nialtoservices/ijs-rails'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0")
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.metadata['yard.run'] = 'yri'
23
+
24
+ spec.add_runtime_dependency 'activerecord', '>= 3.0.0'
25
+ spec.add_runtime_dependency 'activesupport', '>= 3.0.0'
26
+ spec.add_runtime_dependency 'uglifier', '>= 2.3'
27
+
28
+ spec.add_development_dependency 'bundler', '~> 2.1'
29
+ spec.add_development_dependency 'combustion', '~> 1.1'
30
+ spec.add_development_dependency 'guard-rspec', '~> 4.7'
31
+ spec.add_development_dependency 'rake', '~> 13.0'
32
+ spec.add_development_dependency 'rspec', '~> 3.9'
33
+ spec.add_development_dependency 'rspec-html-matchers', '~> 0.9.2'
34
+ spec.add_development_dependency 'rspec-rails', '~> 3.9'
35
+ spec.add_development_dependency 'rubocop', '~> 0.80.0'
36
+ spec.add_development_dependency 'yard', '~> 0.9.24'
37
+ end
data/lib/ijs-rails.rb ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uglifier'
4
+ require 'zlib'
5
+
6
+ require 'ijs-rails/constants'
7
+ require 'ijs-rails/errors/base'
8
+ require 'ijs-rails/errors/script_not_found'
9
+ require 'ijs-rails/action_view_helpers'
10
+ require 'ijs-rails/script'
11
+ require 'ijs-rails/railtie'
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IJSRails
4
+ # Helper methods to include into action view.
5
+ #
6
+ module ActionViewHelpers
7
+ extend ActiveSupport::Concern
8
+
9
+ # Render an HTML <script> tag containing the compiled script.
10
+ #
11
+ # @param script [String] The name of the inline script to render.
12
+ # @param options [Hash] The options to pass to the compiler.
13
+ # @return [String] An HTML <script> tag containing the compiled script.
14
+ #
15
+ def render_ijs(script, options = {})
16
+ type = options.delete(:type)
17
+ compiled_script = IJSRails::Script.new(script, options).compiled.html_safe
18
+ content_tag(:script, compiled_script, type: type)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IJSRails
4
+ # @return [String] The gem's semantic version number.
5
+ #
6
+ VERSION = '0.1.0'
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IJSRails
4
+ # The standard error used as the base class for custom errors implemented by the 'ijs-rails' gem.
5
+ #
6
+ class BaseError < StandardError; end
7
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IJSRails
4
+ # Raised when attempting to use an inline script that can't be found.
5
+ #
6
+ class ScriptNotFound < BaseError
7
+ # @return [String] The name of the script.
8
+ #
9
+ attr_reader :script
10
+
11
+ # Create a new instance of this error.
12
+ #
13
+ # @param script [String] The name of the script.
14
+ #
15
+ def initialize(script)
16
+ @script = script
17
+ end
18
+
19
+ # @return [String] The error message.
20
+ #
21
+ def to_s
22
+ "The inline script '#{script}' could not be found."
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ raise 'ijs-rails is an extension for Rails.' unless defined?(Rails::Railtie)
4
+
5
+ module IJSRails
6
+ # The railtie which ties the gem into the Rails system.
7
+ #
8
+ class Railtie < Rails::Railtie
9
+ initializer 'inline_js.set_configs' do |app|
10
+ app.configure do
11
+ config.inline_js = ActiveSupport::OrderedOptions.new
12
+ config.inline_js.path = Rails.root.join('app/javascript/inline')
13
+ config.inline_js.cache_path = Rails.root.join('tmp/ijs')
14
+ config.inline_js.isolate_scripts = true
15
+ end
16
+ end
17
+
18
+ initializer 'inline_js.action_view_helpers' do
19
+ ActiveSupport.on_load(:action_view) do
20
+ ActionView::Base.include(IJSRails::ActionViewHelpers)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IJSRails
4
+ # Represents an inline script stored in the inline scripts directory.
5
+ #
6
+ class Script
7
+ # @return [String] The name of a script within the inline scripts directory.
8
+ #
9
+ attr_reader :script
10
+
11
+ # @return [Hash] The options to use when compiling the script.
12
+ #
13
+ attr_reader :options
14
+
15
+ # Create a new `Script` instance.
16
+ #
17
+ # @param script [String] The name of a script within the inline scripts directory.
18
+ # @param options [Hash] The options to use when compiling the script.
19
+ #
20
+ def initialize(script, options = {})
21
+ @script = script
22
+ @options = options
23
+
24
+ raise IJSRails::ScriptNotFound, script unless File.file?(file_path)
25
+ end
26
+
27
+ # @return [String] The path to the script file.
28
+ #
29
+ def file_path
30
+ @file_path ||= File.join(Rails.application.config.inline_js.path, "#{script}.js")
31
+ end
32
+
33
+ # @return [String] The fingerprint representing the script and current options.
34
+ #
35
+ def fingerprint
36
+ @fingerprint ||= Zlib.crc32(options.to_json + File.read(file_path)).to_s
37
+ end
38
+
39
+ # @return [String] The path to the compiled script in the cache directory.
40
+ #
41
+ def cache_file_path
42
+ @cache_file_path ||= File.join(Rails.application.config.inline_js.cache_path, "#{script}-#{fingerprint}.min.js")
43
+ end
44
+
45
+ # @return [Boolean] `true` when the cache contains a copy of the compiled script, `false` otherwise.
46
+ #
47
+ def cached?
48
+ File.file?(cache_file_path)
49
+ end
50
+
51
+ # @return [Boolean] `true` when the script should be wrapped isolated in an anonymous function, `false` otherwise.
52
+ #
53
+ def isolate?
54
+ if options.key?(:isolate)
55
+ options[:isolate]
56
+ else
57
+ Rails.application.config.inline_js.isolate_scripts
58
+ end
59
+ end
60
+
61
+ # Compile the script and cache it for reuse.
62
+ #
63
+ # @return [String] The contents of the compiled script.
64
+ #
65
+ def compile!
66
+ # Compile the script.
67
+ script = File.read(file_path)
68
+ script = "!function(){\n#{script}\n}();" if isolate?
69
+ script = Uglifier.compile(script, options.except(:isolate))
70
+
71
+ # Create the cache directory for the script (unless it already exists).
72
+ cache_directory_path = File.dirname(cache_file_path)
73
+ FileUtils.mkdir_p(cache_directory_path) unless File.directory?(cache_directory_path)
74
+
75
+ # Write the script to the determined cache file path.
76
+ File.write(cache_file_path, script)
77
+
78
+ script
79
+ end
80
+
81
+ # Retrieve the compiled script either from the cache or by compiling it.
82
+ #
83
+ # @return [String] The contents of the compiled script.
84
+ #
85
+ def compiled
86
+ return File.read(cache_file_path) if cached?
87
+
88
+ compile!
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe IJSRails::ActionViewHelpers do
4
+ subject(:view) { ActionController::Base.new.view_context }
5
+
6
+ describe '#render_ijs' do
7
+ let(:name) { 'demo/hello' }
8
+
9
+ subject { view.render_ijs(name) }
10
+
11
+ it 'returns an HTML <script> tag containing the script' do
12
+ expect(subject).to have_tag(:script, text: 'alert("Hello, World!");')
13
+ end
14
+
15
+ context 'when :type is provided' do
16
+ let(:type) { 'application/type' }
17
+
18
+ subject { view.render_ijs(name, type: type) }
19
+
20
+ it 'sets the :type attribute on the <script> element' do
21
+ expect(subject).to have_tag(:script, with: { type: type })
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe IJSRails do
4
+ describe '::VERSION' do
5
+ subject { described_class::VERSION }
6
+
7
+ it 'is semantic' do
8
+ expect(subject).to match(/[0-9]+\.[0-9]+\.[0-9]+/)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,143 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe IJSRails::Script do
4
+ def rails_path(*options)
5
+ Rails.root.join(*options).to_s
6
+ end
7
+
8
+ def add_cache_file(data = '')
9
+ FileUtils.mkdir_p(File.dirname(cache_file_path))
10
+ File.write(cache_file_path, data)
11
+ end
12
+
13
+ def read_cache_file
14
+ File.read(cache_file_path)
15
+ end
16
+
17
+ def remove_cache_file
18
+ FileUtils.rm_f(cache_file_path)
19
+ end
20
+
21
+ after { remove_cache_file }
22
+
23
+ let(:name) { 'demo/hello' }
24
+ let(:fingerprint) { '453138908' }
25
+ let(:file_name) { "#{name}.js" }
26
+ let(:file_path) { rails_path('app/javascript/inline', file_name) }
27
+ let(:cache_file_name) { "#{name}-#{fingerprint}.min.js" }
28
+ let(:cache_file_path) { rails_path('tmp/ijs', cache_file_name) }
29
+ let(:options) do
30
+ {}
31
+ end
32
+
33
+ subject(:script) { described_class.new(name, options) }
34
+
35
+ it { is_expected.to respond_to(:script) }
36
+ it { is_expected.to respond_to(:options) }
37
+
38
+ describe '#file_path' do
39
+ subject { script.file_path }
40
+
41
+ it 'returns the correct path' do
42
+ expect(subject).to eq(file_path)
43
+ end
44
+
45
+ it 'uses the path from the Rails configuration' do
46
+ expect(Rails.application.config.inline_js).to receive(:path).and_return(rails_path('app/ijs'))
47
+ expect(subject).to eq(rails_path('app/ijs', file_name))
48
+ end
49
+ end
50
+
51
+ describe '#fingerprint' do
52
+ subject { script.fingerprint }
53
+
54
+ it 'computes the correct fingerprint' do
55
+ expect(subject).to eq(fingerprint)
56
+ end
57
+ end
58
+
59
+ describe '#cache_file_path' do
60
+ subject { script.cache_file_path }
61
+
62
+ it 'returns the correct path' do
63
+ expect(subject).to eq(cache_file_path)
64
+ end
65
+
66
+ it 'uses the path from the Rails configuration' do
67
+ expect(Rails.application.config.inline_js).to receive(:cache_path).and_return(rails_path('tmp/cache/ijs'))
68
+ expect(subject).to eq(rails_path('tmp/cache/ijs', cache_file_name))
69
+ end
70
+ end
71
+
72
+ describe '#cached?' do
73
+ subject { script.cached? }
74
+
75
+ context 'when not cached' do
76
+ it { is_expected.to eq(false) }
77
+ end
78
+
79
+ context 'when cached' do
80
+ before { add_cache_file }
81
+
82
+ it { is_expected.to eq(true) }
83
+ end
84
+ end
85
+
86
+ describe '#isolate?' do
87
+ subject { script.isolate? }
88
+
89
+ context 'with defaults' do
90
+ it { is_expected.to eq(true) }
91
+ end
92
+
93
+ context 'when #options[:isolate] is true' do
94
+ let(:options) do
95
+ { isolate: true }
96
+ end
97
+
98
+ it { is_expected.to eq(true) }
99
+ end
100
+
101
+ context 'when #options[:isolate] is false' do
102
+ let(:options) do
103
+ { isolate: false }
104
+ end
105
+
106
+ it { is_expected.to eq(false) }
107
+ end
108
+ end
109
+
110
+ describe '#compile!' do
111
+ let(:result) { 'alert("Hello, World!");' }
112
+
113
+ subject { script.compile! }
114
+
115
+ it 'returns the compiled script' do
116
+ expect(subject).to eq(result)
117
+ end
118
+
119
+ it 'caches the compiled script' do
120
+ subject
121
+ expect(read_cache_file).to eq(result)
122
+ end
123
+ end
124
+
125
+ describe '#compiled' do
126
+ subject { script.compiled }
127
+
128
+ context 'when cached' do
129
+ before { add_cache_file '// JS' }
130
+
131
+ it 'returns the contents of the cached file' do
132
+ expect(subject).to eq('// JS')
133
+ end
134
+ end
135
+
136
+ context 'when not cached' do
137
+ it 'compiles the script' do
138
+ expect(script).to receive(:compile!)
139
+ subject
140
+ end
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,5 @@
1
+ //
2
+ // This is a sample JavaScript file to say hello!
3
+ //
4
+
5
+ alert('Hello, World!');
@@ -0,0 +1,5 @@
1
+ //
2
+ // This is a sample JavaScript file to say hello!
3
+ //
4
+
5
+ alert('Hello, World!');
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ ENV['RAILS_ENV'] ||= 'test'
4
+
5
+ require 'bundler'
6
+ require 'combustion'
7
+
8
+ Bundler.require :default, :test
9
+ Combustion.initialize! :action_controller, :action_view
10
+
11
+ # Prevent database truncation if the environment is production.
12
+ abort 'The Rails environment is running in production mode!' if Rails.env.production?
13
+
14
+ require 'rspec/rails'
15
+ require 'spec_helper'
16
+
17
+ # Add additional requires below this line. Rails is not loaded until this point!
18
+
19
+ # Requires supporting ruby files with custom matchers and macros, etc, in spec/support/ and its subdirectories.
20
+ # Files matching `spec/**/*_spec.rb` are run as spec files by default.
21
+ # This means that files in spec/support that end in _spec.rb will both be required and run as specs, causing the specs
22
+ # to be run twice.
23
+ # It is recommended that you do not name files matching this glob to end with _spec.rb.
24
+ # You can configure this pattern with the --pattern option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
25
+ #
26
+ # The following line is provided for convenience purposes.
27
+ # It has the downside of increasing the boot-up time by auto-requiring all files in the support directory.
28
+ # Alternatively, in the individual `*_spec.rb` files, manually require only the support files necessary.
29
+ #
30
+ Dir[Rails.root.join('../support/**/*.rb')].sort.each { |f| require f }
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Conventionally, all specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
4
+ #
5
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this file to always be loaded, without
6
+ # a need to explicitly require it in any files.
7
+ #
8
+ # Given that it is always loaded, you are encouraged to keep this file as light-weight as possible.
9
+ # Requiring heavyweight dependencies from this file will add to the boot time of your test suite on EVERY test run,
10
+ # even for an individual file that may not need all of that loaded.
11
+ # Instead, consider making a separate helper file that requires the additional dependencies and performs the additional
12
+ # setup, and require it from the spec files that actually need it.
13
+ #
14
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
15
+
16
+ RSpec.configure do |config|
17
+ # rspec-expectations config goes here. You can use an alternate assertion/expectation library such as wrong or the
18
+ # stdlib/minitest assertions if you prefer.
19
+ config.expect_with :rspec do |expectations|
20
+ # This option will default to `true` in RSpec 4.
21
+ # It makes the `description` and `failure_message` of custom matchers include text for helper methods defined using
22
+ # `chain`, e.g.:
23
+ # be_bigger_than(2).and_smaller_than(4).description
24
+ # # => "be bigger than 2 and smaller than 4"
25
+ # ...rather than:
26
+ # # => "be bigger than 2"
27
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
28
+ end
29
+
30
+ # rspec-mocks config goes here.
31
+ # You can use an alternate test double library (such as bogus or mocha) by changing the `mock_with` option here.
32
+ config.mock_with :rspec do |mocks|
33
+ # Prevents you from mocking or stubbing a method that does not exist on a real object.
34
+ # This is generally recommended, and will default to `true` in RSpec 4.
35
+ mocks.verify_partial_doubles = true
36
+ end
37
+
38
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will have no way to turn it off -- the option
39
+ # exists only for backwards compatibility in RSpec 3).
40
+ # It causes shared context metadata to be inherited by the metadata hash of host groups and examples, rather than
41
+ # triggering implicit auto-inclusion in groups with matching metadata.
42
+ config.shared_context_metadata_behavior = :apply_to_host_groups
43
+
44
+ # This allows you to limit a spec run to individual examples or groups you care about by tagging them with `:focus`
45
+ # metadata.
46
+ # When nothing is tagged with `:focus`, all examples get run.
47
+ # RSpec also provides aliases for `it`, `describe`, and `context` that include `:focus` metadata: `fit`, `fdescribe`
48
+ # and `fcontext`, respectively.
49
+ config.filter_run_when_matching :focus
50
+
51
+ # Allows RSpec to persist some state between runs in order to support the `--only-failures` and `--next-failure` CLI
52
+ # options.
53
+ # We recommend you configure your source control system to ignore this file.
54
+ config.example_status_persistence_file_path = 'tmp/rspec_history.txt'
55
+
56
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
57
+ # For more details, see:
58
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
59
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
60
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
61
+ config.disable_monkey_patching!
62
+
63
+ # Many RSpec users commonly either run the entire suite or an individual file, and it's useful to allow more verbose
64
+ # output when running an individual spec file.
65
+ if config.files_to_run.one?
66
+ # Use the documentation formatter for detailed output, unless a formatter has already been configured (e.g. via a
67
+ # command-line flag).
68
+ config.default_formatter = 'doc'
69
+ end
70
+
71
+ # Print the 10 slowest examples and example groups at the end of the spec run, to help surface which specs are running
72
+ # particularly slow.
73
+ config.profile_examples = 10
74
+
75
+ # Run specs in random order to surface order dependencies. If you find an order dependency and want to debug it, you
76
+ # can fix the order by providing the seed, which is printed after each run.
77
+ # --seed 1234
78
+ config.order = :random
79
+
80
+ # Seed global randomization in this process using the `--seed` CLI option.
81
+ # Setting this allows you to use `--seed` to deterministically reproduce test failures related to randomization by
82
+ # passing the same `--seed` value as the one that triggered the failure.
83
+ Kernel.srand config.seed
84
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rspec-html-matchers'
4
+
5
+ RSpec.configure do |config|
6
+ config.include RSpecHtmlMatchers
7
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec path: '../../'
6
+
7
+ gem 'activerecord', '~> 6.0.0'
8
+ gem 'activesupport', '~> 6.0.0'
metadata ADDED
@@ -0,0 +1,250 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ijs-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nialto Services
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-02-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 3.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: uglifier
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '2.3'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '2.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: combustion
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '4.7'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '4.7'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '13.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '13.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.9'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.9'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec-html-matchers
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 0.9.2
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 0.9.2
139
+ - !ruby/object:Gem::Dependency
140
+ name: rspec-rails
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '3.9'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '3.9'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rubocop
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: 0.80.0
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: 0.80.0
167
+ - !ruby/object:Gem::Dependency
168
+ name: yard
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: 0.9.24
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: 0.9.24
181
+ description:
182
+ email:
183
+ - support@nialtoservices.co.uk
184
+ executables: []
185
+ extensions: []
186
+ extra_rdoc_files: []
187
+ files:
188
+ - ".gitignore"
189
+ - ".rbenv-gemsets"
190
+ - ".rspec"
191
+ - ".rubocop.yml"
192
+ - ".ruby-version"
193
+ - ".travis.yml"
194
+ - Gemfile
195
+ - Gemfile.lock
196
+ - Guardfile
197
+ - LICENSE
198
+ - README.md
199
+ - Rakefile
200
+ - config.ru
201
+ - ijs-rails.gemspec
202
+ - lib/ijs-rails.rb
203
+ - lib/ijs-rails/action_view_helpers.rb
204
+ - lib/ijs-rails/constants.rb
205
+ - lib/ijs-rails/errors/base.rb
206
+ - lib/ijs-rails/errors/script_not_found.rb
207
+ - lib/ijs-rails/railtie.rb
208
+ - lib/ijs-rails/script.rb
209
+ - spec/ijs-rails/action_view_helpers_spec.rb
210
+ - spec/ijs-rails/constants_spec.rb
211
+ - spec/ijs-rails/script_spec.rb
212
+ - spec/internal/app/ijs/demo/hello.js
213
+ - spec/internal/app/javascript/inline/demo/hello.js
214
+ - spec/rails_helper.rb
215
+ - spec/spec_helper.rb
216
+ - spec/support/html-matchers.rb
217
+ - travis/gemfiles/6.0.gemfile
218
+ homepage: https://github.com/nialtoservices/ijs-rails
219
+ licenses:
220
+ - MIT
221
+ metadata:
222
+ yard.run: yri
223
+ post_install_message:
224
+ rdoc_options: []
225
+ require_paths:
226
+ - lib
227
+ required_ruby_version: !ruby/object:Gem::Requirement
228
+ requirements:
229
+ - - ">="
230
+ - !ruby/object:Gem::Version
231
+ version: '0'
232
+ required_rubygems_version: !ruby/object:Gem::Requirement
233
+ requirements:
234
+ - - ">="
235
+ - !ruby/object:Gem::Version
236
+ version: '0'
237
+ requirements: []
238
+ rubygems_version: 3.0.3
239
+ signing_key:
240
+ specification_version: 4
241
+ summary: Inline JavaScript helper for Ruby on Rails.
242
+ test_files:
243
+ - spec/ijs-rails/action_view_helpers_spec.rb
244
+ - spec/ijs-rails/constants_spec.rb
245
+ - spec/ijs-rails/script_spec.rb
246
+ - spec/internal/app/ijs/demo/hello.js
247
+ - spec/internal/app/javascript/inline/demo/hello.js
248
+ - spec/rails_helper.rb
249
+ - spec/spec_helper.rb
250
+ - spec/support/html-matchers.rb