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

Load all resources from Ember CLI Rails #36

Merged
merged 3 commits into from
May 24, 2017
Merged
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
28 changes: 15 additions & 13 deletions lib/percy/capybara/loaders/ember_cli_rails_loader.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
require 'percy/capybara/loaders/sprockets_loader'
require 'set'

module Percy
module Capybara
module Loaders
class EmberCliRailsLoader < SprocketsLoader
attr_reader :mounted_apps

EMBER_ASSETS_DIR = 'assets'.freeze

def initialize(mounted_apps, options = {})
super(options)

Expand All @@ -16,25 +15,28 @@ def initialize(mounted_apps, options = {})
end

def build_resources
resources = []
resources = super # adds sprockets resources first

@mounted_apps.map do |app_name, mount_path|
# public assets path for this particular ember app. If the app is mounted on /admin
# the output would be: /admin/assets
base_assets_url = File.join(mount_path, EMBER_ASSETS_DIR)
sprockets_resource_urls = resources.collect(&:resource_url)
loaded_resource_urls = Set.new(sprockets_resource_urls)

# full path on disk to the assets for this ember app
@mounted_apps.map do |app_name, mount_path|
# full path on disk to this ember app
# e.g. /Users/djones/Code/rails-ember-app/tmp/ember-cli/apps/frontend
dist_path = _dist_path_for_app(app_name)

# full path to the directory on disk where ember stores assets for this ember app
# e.g. /Users/djones/Code/rails-ember-app/tmp/ember-cli/apps/frontend/assets
ember_assets_path = File.join(dist_path, EMBER_ASSETS_DIR)
resources_from_dir = _resources_from_dir(dist_path, base_url: mount_path)

resources_from_dir.each do |resource|
# avoid loading in duplicate resource_urls
next if loaded_resource_urls.include? resource.resource_url

resources += _resources_from_dir(ember_assets_path, base_url: base_assets_url)
resources << resource
loaded_resource_urls << resource.resource_url
end
end

resources += super # adds sprockets resources from Rails
resources
end

def _dist_path_for_app(app_name)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 8 additions & 7 deletions spec/lib/percy/capybara/loaders/ember_cli_rails_loader_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'set'

RSpec.describe Percy::Capybara::Loaders::EmberCliRailsLoader do
let(:assets_dir) { File.expand_path('../../client/ember_test_data', __FILE__) }
let(:mounted_apps) { {frontend: ''} }
Expand Down Expand Up @@ -62,14 +64,13 @@
end

it 'builds the expected resources' do
expected_urls = loader.build_resources.collect(&:resource_url)
expected_url = loader._uri_join(
mount_path,
described_class::EMBER_ASSETS_DIR,
"percy-#{ember_app}.svg",
)
built_urls = Set.new(loader.build_resources.collect(&:resource_url))

expected_urls = Set.new
expected_urls << loader._uri_join(mount_path, "/assets/percy-#{ember_app}.svg")
expected_urls << loader._uri_join(mount_path, "/percy-#{ember_app}-public.svg")

expect(expected_urls).to include(expected_url)
expect(expected_urls.subset?(built_urls)).to be true
end
end
end
Expand Down