Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add memoization #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/.bundle
/log/*.log
/tmp
.idea
Gemfile.lock
log
.jhw-cache
target
*.gem
*.gem
/.rakeTasks
/spec/internal/log/
/.byebug_history
/spec/internal/db/combustion_test.sqlite
/spec/internal/tmp/
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
source 'http://rubygems.org'

gemspec

gem "rails", "~> 5.0"
182 changes: 182 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
module KnockoutAssets
def knockout_assets(options = {})
module KnockoutAssetsHelper
include ::Memery

memoize def knockout_assets(options = {})
options = {
exclude: nil,
preload: true,
include: /.*\.(png|gif|jpg|jpeg|bmp|svg)/,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these file extensions be anchored to the end of path as /.*\.(png|...|svg)$/?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also you can probably drop the .* at the start

}.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
Expand Down
5 changes: 2 additions & 3 deletions app/views/_knockout_assets.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@
<% if preload
asset_files.each { |k, v| %>
<img src="<%= v %>" alt="<%= k %>" style="display: none;"/>
<% }
end
%>
<% } %>
<% end %>
6 changes: 6 additions & 0 deletions knockout-assets.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 7 additions & 5 deletions lib/knockout/assets/engine.rb
Original file line number Diff line number Diff line change
@@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On reload, you presumably want to bust the knockout_assets cache - will that happen?

end
end
end
end

ActiveSupport.on_load :action_controller do
::ActionController::Base.helper(KnockoutAssets)
end
2 changes: 1 addition & 1 deletion lib/knockout/assets/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module KnockoutAssets
VERSION = '0.0.5'
VERSION = '1.0.0'
end
1 change: 1 addition & 0 deletions spec/internal/app/assets/config/manifest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//= link_tree ../img
Binary file added spec/internal/app/assets/img/ccta_logo2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added spec/internal/app/assets/img/cifas_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added spec/internal/app/assets/img/paypoint-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added spec/internal/app/assets/img/positive-ssl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added spec/internal/app/assets/img/ssl-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added spec/internal/app/assets/img/tel-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added spec/internal/app/assets/img/wp-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions spec/internal/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ApplicationController < ActionController::Base
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't comment on the above assets in the GH diff, but if this is a public gem, I don't think we should include other companies' logos

include KnockoutAssets

def show
render "show"
end
end
2 changes: 2 additions & 0 deletions spec/internal/app/views/application/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%= knockout_assets %>
<% knockout_assets # Check calling it a second time is memoized %>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it valid to call it twice? Why/how does that happen? Should it be an error instead?

15 changes: 15 additions & 0 deletions spec/internal/config/application.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions spec/internal/config/database.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test:
adapter: sqlite3
database: db/combustion_test.sqlite
5 changes: 5 additions & 0 deletions spec/internal/config/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

Rails.application.routes.draw do
get "/knockout-assets", to: "application#show"
end
46 changes: 46 additions & 0 deletions spec/requests/knockout_assets_spec.rb
Original file line number Diff line number Diff line change
@@ -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)
<script type="text/javascript">
var knockout_assets = {};

knockout_assets['ccta_logo2.png'] = '/assets/ccta_logo2-aab6037854ecc753e233d14cb862721ebe2cfb6fcdf5061f19c61d75f7618fc6.png';
knockout_assets['cifas_logo.png'] = '/assets/cifas_logo-f63aeee7ca3531c33746ef28581f69d177b337823e59ca46cb6a9655ef8772e7.png';
knockout_assets['paypoint-logo.png'] = '/assets/paypoint-logo-0b14eefd4fee7289e11fa9a935b7cac162111577d8158192b157c00e0f9bc882.png';
knockout_assets['positive-ssl.png'] = '/assets/positive-ssl-927f00f9916794da35a191ca6b23a46d19c6ea4a2b8a46644705de46654596e5.png';
knockout_assets['wp-logo.png'] = '/assets/wp-logo-5a322035ac8a7d15b65a6a53600fb774977b62008ab5544ecbd650c91120ba57.png';
knockout_assets['tel-icon.png'] = '/assets/tel-icon-c8756e0413938fd270cf8c61463c6440e392876b753d4f38c0fd5e764fc8a6ad.png';
knockout_assets['ssl-logo.png'] = '/assets/ssl-logo-e004c2aaaee7c30f03e2c0441cff3a5e640e2945550d636ccdd47caabf60e420.png';

ko.bindingHandlers.img = {
init: function (element, valueAccessor) {
var str = ko.utils.unwrapObservable(valueAccessor());
$(element).attr('src', knockout_assets[str]);
}
};
</script>

<img src="/assets/ccta_logo2-aab6037854ecc753e233d14cb862721ebe2cfb6fcdf5061f19c61d75f7618fc6.png" alt="ccta_logo2.png" style="display: none;"/>
<img src="/assets/cifas_logo-f63aeee7ca3531c33746ef28581f69d177b337823e59ca46cb6a9655ef8772e7.png" alt="cifas_logo.png" style="display: none;"/>
<img src="/assets/paypoint-logo-0b14eefd4fee7289e11fa9a935b7cac162111577d8158192b157c00e0f9bc882.png" alt="paypoint-logo.png" style="display: none;"/>
<img src="/assets/positive-ssl-927f00f9916794da35a191ca6b23a46d19c6ea4a2b8a46644705de46654596e5.png" alt="positive-ssl.png" style="display: none;"/>
<img src="/assets/wp-logo-5a322035ac8a7d15b65a6a53600fb774977b62008ab5544ecbd650c91120ba57.png" alt="wp-logo.png" style="display: none;"/>
<img src="/assets/tel-icon-c8756e0413938fd270cf8c61463c6440e392876b753d4f38c0fd5e764fc8a6ad.png" alt="tel-icon.png" style="display: none;"/>
<img src="/assets/ssl-logo-e004c2aaaee7c30f03e2c0441cff3a5e640e2945550d636ccdd47caabf60e420.png" alt="ssl-logo.png" style="display: none;"/>

STR
end

it "is memoized" do
expect(::Rails.application.config.assets).to receive(:paths).once.and_call_original
get "/knockout-assets"
end
end
end
12 changes: 12 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -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"