diff --git a/.gitignore b/.gitignore index 9c9c1d4..6570255 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,7 @@ -/.bundle -/log/*.log -/tmp .idea -Gemfile.lock -log -.jhw-cache -target -*.gem \ No newline at end of file +*.gem +/.rakeTasks +/spec/internal/log/ +/.byebug_history +/spec/internal/db/combustion_test.sqlite +/spec/internal/tmp/ diff --git a/Gemfile b/Gemfile index d65e2a6..c1f8330 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ source 'http://rubygems.org' gemspec + +gem "rails", "~> 5.0" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..e8c5a8b --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,182 @@ +PATH + remote: . + specs: + knockout-assets (1.0.0) + memery + rails (> 5.0) + +GEM + remote: http://rubygems.org/ + specs: + actioncable (5.2.8.1) + actionpack (= 5.2.8.1) + nio4r (~> 2.0) + websocket-driver (>= 0.6.1) + actionmailer (5.2.8.1) + actionpack (= 5.2.8.1) + actionview (= 5.2.8.1) + activejob (= 5.2.8.1) + mail (~> 2.5, >= 2.5.4) + rails-dom-testing (~> 2.0) + actionpack (5.2.8.1) + actionview (= 5.2.8.1) + activesupport (= 5.2.8.1) + rack (~> 2.0, >= 2.0.8) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.0.2) + actionview (5.2.8.1) + activesupport (= 5.2.8.1) + builder (~> 3.1) + erubi (~> 1.4) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.0.3) + activejob (5.2.8.1) + activesupport (= 5.2.8.1) + globalid (>= 0.3.6) + activemodel (5.2.8.1) + activesupport (= 5.2.8.1) + activerecord (5.2.8.1) + activemodel (= 5.2.8.1) + activesupport (= 5.2.8.1) + arel (>= 9.0) + activestorage (5.2.8.1) + actionpack (= 5.2.8.1) + activerecord (= 5.2.8.1) + marcel (~> 1.0.0) + activesupport (5.2.8.1) + concurrent-ruby (~> 1.0, >= 1.0.2) + i18n (>= 0.7, < 2) + minitest (~> 5.1) + tzinfo (~> 1.1) + arel (9.0.0) + builder (3.2.4) + byebug (11.1.3) + coderay (1.1.3) + combustion (1.3.7) + activesupport (>= 3.0.0) + railties (>= 3.0.0) + thor (>= 0.14.6) + concurrent-ruby (1.1.10) + crass (1.0.6) + date (3.3.3) + diff-lcs (1.5.0) + erubi (1.12.0) + globalid (1.0.1) + activesupport (>= 5.0) + i18n (1.12.0) + concurrent-ruby (~> 1.0) + loofah (2.19.1) + crass (~> 1.0.2) + nokogiri (>= 1.5.9) + mail (2.8.0.1) + mini_mime (>= 0.1.1) + net-imap + net-pop + net-smtp + marcel (1.0.2) + memery (1.4.1) + ruby2_keywords (~> 0.0.2) + method_source (1.0.0) + mini_mime (1.1.2) + minitest (5.17.0) + net-imap (0.3.4) + date + net-protocol + net-pop (0.1.2) + net-protocol + net-protocol (0.2.1) + timeout + net-smtp (0.3.3) + net-protocol + nio4r (2.5.8) + nokogiri (1.14.0-x86_64-linux) + racc (~> 1.4) + pry (0.14.2) + coderay (~> 1.1) + method_source (~> 1.0) + pry-byebug (3.10.1) + byebug (~> 11.0) + pry (>= 0.13, < 0.15) + racc (1.6.2) + rack (2.2.6.2) + rack-test (2.0.2) + rack (>= 1.3) + rails (5.2.8.1) + actioncable (= 5.2.8.1) + actionmailer (= 5.2.8.1) + actionpack (= 5.2.8.1) + actionview (= 5.2.8.1) + activejob (= 5.2.8.1) + activemodel (= 5.2.8.1) + activerecord (= 5.2.8.1) + activestorage (= 5.2.8.1) + activesupport (= 5.2.8.1) + bundler (>= 1.3.0) + railties (= 5.2.8.1) + sprockets-rails (>= 2.0.0) + rails-dom-testing (2.0.3) + activesupport (>= 4.2.0) + nokogiri (>= 1.6) + rails-html-sanitizer (1.4.4) + loofah (~> 2.19, >= 2.19.1) + railties (5.2.8.1) + actionpack (= 5.2.8.1) + activesupport (= 5.2.8.1) + method_source + rake (>= 0.8.7) + thor (>= 0.19.0, < 2.0) + rake (13.0.6) + rspec (3.12.0) + rspec-core (~> 3.12.0) + rspec-expectations (~> 3.12.0) + rspec-mocks (~> 3.12.0) + rspec-core (3.12.0) + rspec-support (~> 3.12.0) + rspec-expectations (3.12.2) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.12.0) + rspec-mocks (3.12.3) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.12.0) + rspec-rails (5.1.2) + actionpack (>= 5.2) + activesupport (>= 5.2) + railties (>= 5.2) + rspec-core (~> 3.10) + rspec-expectations (~> 3.10) + rspec-mocks (~> 3.10) + rspec-support (~> 3.10) + rspec-support (3.12.0) + ruby2_keywords (0.0.5) + sprockets (4.2.0) + concurrent-ruby (~> 1.0) + rack (>= 2.2.4, < 4) + sprockets-rails (3.4.2) + actionpack (>= 5.2) + activesupport (>= 5.2) + sprockets (>= 3.0.0) + sqlite3 (1.6.0-x86_64-linux) + thor (1.2.1) + thread_safe (0.3.6) + timeout (0.3.1) + tzinfo (1.2.10) + thread_safe (~> 0.1) + websocket-driver (0.7.5) + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.5) + +PLATFORMS + x86_64-linux + +DEPENDENCIES + combustion + knockout-assets! + pry-byebug + rails (~> 5.0) + rspec + rspec-rails (~> 5.0) + sqlite3 + +BUNDLED WITH + 2.4.3 diff --git a/app/helpers/knockout_assets.rb b/app/helpers/knockout_assets_helper.rb similarity index 77% rename from app/helpers/knockout_assets.rb rename to app/helpers/knockout_assets_helper.rb index 46b1647..82c480d 100644 --- a/app/helpers/knockout_assets.rb +++ b/app/helpers/knockout_assets_helper.rb @@ -1,5 +1,7 @@ -module KnockoutAssets - def knockout_assets(options = {}) +module KnockoutAssetsHelper + include ::Memery + + memoize def knockout_assets(options = {}) options = { exclude: nil, preload: true, @@ -7,7 +9,7 @@ def knockout_assets(options = {}) }.merge(options) files = {} - Rails.application.config.assets.paths.each { |p| + ::Rails.application.config.assets.paths.each { |p| Dir["#{p}/**/*"].select { |f| f =~ options[:include] }.each { |f| item_path = f[p.length+1..-1] if !options[:exclude] || options[:exclude] !~ item_path diff --git a/app/views/_knockout_assets.erb b/app/views/_knockout_assets.erb index d0b1f85..6523dff 100644 --- a/app/views/_knockout_assets.erb +++ b/app/views/_knockout_assets.erb @@ -18,6 +18,5 @@ <% if preload asset_files.each { |k, v| %> <%= k %> - <% } - end - %> \ No newline at end of file + <% } %> +<% end %> diff --git a/knockout-assets.gemspec b/knockout-assets.gemspec index 64a9be8..ed732ea 100644 --- a/knockout-assets.gemspec +++ b/knockout-assets.gemspec @@ -17,4 +17,10 @@ Gem::Specification.new do |s| s.require_paths = ['lib'] s.add_dependency 'rails', '> 5.0' + s.add_dependency 'memery' + s.add_development_dependency 'combustion' + s.add_development_dependency 'pry-byebug' + s.add_development_dependency 'rspec-rails', '~> 5.0' + s.add_development_dependency 'rspec' + s.add_development_dependency 'sqlite3' end diff --git a/lib/knockout/assets/engine.rb b/lib/knockout/assets/engine.rb index 1e97e68..b2275e7 100644 --- a/lib/knockout/assets/engine.rb +++ b/lib/knockout/assets/engine.rb @@ -1,11 +1,13 @@ require 'rails/engine' -require_relative '../../../app/helpers/knockout_assets' +require 'memery' module KnockoutAssets class Engine < ::Rails::Engine + config.to_prepare do + ActiveSupport.on_load :action_controller do + include KnockoutAssetsHelper + helper_method :knockout_assets + end + end end end - -ActiveSupport.on_load :action_controller do - ::ActionController::Base.helper(KnockoutAssets) -end diff --git a/lib/knockout/assets/version.rb b/lib/knockout/assets/version.rb index bcda7da..466f205 100644 --- a/lib/knockout/assets/version.rb +++ b/lib/knockout/assets/version.rb @@ -1,3 +1,3 @@ module KnockoutAssets - VERSION = '0.0.5' + VERSION = '1.0.0' end diff --git a/spec/internal/app/assets/config/manifest.js b/spec/internal/app/assets/config/manifest.js new file mode 100644 index 0000000..166f1c6 --- /dev/null +++ b/spec/internal/app/assets/config/manifest.js @@ -0,0 +1 @@ +//= link_tree ../img diff --git a/spec/internal/app/assets/img/ccta_logo2.png b/spec/internal/app/assets/img/ccta_logo2.png new file mode 100644 index 0000000..ef508f4 Binary files /dev/null and b/spec/internal/app/assets/img/ccta_logo2.png differ diff --git a/spec/internal/app/assets/img/cifas_logo.png b/spec/internal/app/assets/img/cifas_logo.png new file mode 100644 index 0000000..9ef7156 Binary files /dev/null and b/spec/internal/app/assets/img/cifas_logo.png differ diff --git a/spec/internal/app/assets/img/paypoint-logo.png b/spec/internal/app/assets/img/paypoint-logo.png new file mode 100644 index 0000000..ad5adb3 Binary files /dev/null and b/spec/internal/app/assets/img/paypoint-logo.png differ diff --git a/spec/internal/app/assets/img/positive-ssl.png b/spec/internal/app/assets/img/positive-ssl.png new file mode 100644 index 0000000..1664300 Binary files /dev/null and b/spec/internal/app/assets/img/positive-ssl.png differ diff --git a/spec/internal/app/assets/img/ssl-logo.png b/spec/internal/app/assets/img/ssl-logo.png new file mode 100644 index 0000000..81abe14 Binary files /dev/null and b/spec/internal/app/assets/img/ssl-logo.png differ diff --git a/spec/internal/app/assets/img/tel-icon.png b/spec/internal/app/assets/img/tel-icon.png new file mode 100644 index 0000000..383c4ab Binary files /dev/null and b/spec/internal/app/assets/img/tel-icon.png differ diff --git a/spec/internal/app/assets/img/wp-logo.png b/spec/internal/app/assets/img/wp-logo.png new file mode 100644 index 0000000..251b3fb Binary files /dev/null and b/spec/internal/app/assets/img/wp-logo.png differ diff --git a/spec/internal/app/controllers/application_controller.rb b/spec/internal/app/controllers/application_controller.rb new file mode 100644 index 0000000..b97f955 --- /dev/null +++ b/spec/internal/app/controllers/application_controller.rb @@ -0,0 +1,7 @@ +class ApplicationController < ActionController::Base + include KnockoutAssets + + def show + render "show" + end +end diff --git a/spec/internal/app/views/application/show.html.erb b/spec/internal/app/views/application/show.html.erb new file mode 100644 index 0000000..7b0e335 --- /dev/null +++ b/spec/internal/app/views/application/show.html.erb @@ -0,0 +1,2 @@ +<%= knockout_assets %> +<% knockout_assets # Check calling it a second time is memoized %> diff --git a/spec/internal/config/application.rb b/spec/internal/config/application.rb new file mode 100644 index 0000000..e7bcbfa --- /dev/null +++ b/spec/internal/config/application.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require File.expand_path("boot", __dir__) + +Bundler.require(*Rails.groups) + +require "sprockets/railtie" + +class Application < ::Rails::Application + # Enable the asset pipeline + config.assets.enabled = true + + # Version of your assets, change this if you want to expire all your assets + config.assets.version = "1.0" +end diff --git a/spec/internal/config/database.yml b/spec/internal/config/database.yml new file mode 100644 index 0000000..b978119 --- /dev/null +++ b/spec/internal/config/database.yml @@ -0,0 +1,3 @@ +test: + adapter: sqlite3 + database: db/combustion_test.sqlite diff --git a/spec/internal/config/routes.rb b/spec/internal/config/routes.rb new file mode 100644 index 0000000..3046393 --- /dev/null +++ b/spec/internal/config/routes.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +Rails.application.routes.draw do + get "/knockout-assets", to: "application#show" +end diff --git a/spec/requests/knockout_assets_spec.rb b/spec/requests/knockout_assets_spec.rb new file mode 100644 index 0000000..c66a40f --- /dev/null +++ b/spec/requests/knockout_assets_spec.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe KnockoutAssets, type: :request do + describe "#show" do + it "returns the knockout assets" do + get "/knockout-assets" + result = response.body + expect(result).to eq(<<~STR) + + + ccta_logo2.png + cifas_logo.png + paypoint-logo.png + positive-ssl.png + wp-logo.png + tel-icon.png + ssl-logo.png + + STR + end + + it "is memoized" do + expect(::Rails.application.config.assets).to receive(:paths).once.and_call_original + get "/knockout-assets" + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..ac489c5 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +Bundler.require :default, :test + +require "rails/all" +require "combustion" +require "knockout-assets" +require "pry-byebug" + +Combustion.initialize! :action_controller, :action_view, :sprockets + +require "rspec/rails"