Skip to content

Commit

Permalink
Adds support for rails 4.0 beta
Browse files Browse the repository at this point in the history
Improves static pages scaffolding (preview now works with debug mode on
asset pipeline)
Changes the autosync in (>= 3.2), it no longer runs on app boot. Now
runs after assets:precompile is done.
Version bump
  • Loading branch information
epetre committed Feb 4, 2013
1 parent c5c0653 commit a8fac58
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -6,3 +6,5 @@ pkg/*
*.DS_Store
.rspec
*.swp
*.config-store
*.config
4 changes: 2 additions & 2 deletions Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
trackman (0.6.12)
trackman (0.6.13)
json
nokogiri
rack
Expand All @@ -17,7 +17,7 @@ GEM
json (1.7.5)
mime-types (1.19)
multi_json (1.3.6)
nokogiri (1.5.5)
nokogiri (1.5.6)
rack (1.4.1)
rake (0.9.2.2)
rest-client (1.6.7)
Expand Down
3 changes: 1 addition & 2 deletions Rakefile
@@ -1,14 +1,13 @@
require "bundler/gem_tasks"
require 'rspec/core/rake_task'


desc "creates a new app to the server and outputs the credentials"
task :create_app do
require 'trackman'
RemoteAsset = Trackman::Assets::RemoteAsset

user = ENV['HEROKU_USERNAME']
pass = ENV['HEROKU_PASSWORD']
pass# = ENV['HEROKU_PASSWORD']
server = RemoteAsset.class_variable_get :@@server

url = "http://#{user}:#{pass}@#{server}/heroku/resources"
Expand Down
30 changes: 24 additions & 6 deletions lib/trackman/scaffold/content_saver.rb
Expand Up @@ -33,13 +33,23 @@ def remove selector, &predicate
end

protected
def remove_default_nodes doc
nodes = []
nodes = nodes.concat doc.search('script').select{|n| n['src'] && n['src'].include?('/assets') && !n['src'].include?('application') }
nodes = nodes.concat doc.search('link').select{|n| n['href'] && n['href'].include?('/assets') && !n['href'].include?('application') }
nodes.each{|n| n.remove }

doc
end

This comment has been minimized.

Copy link
@DouweM

DouweM Feb 6, 2013

What's happening here? Why are all asset references except to /assets/application.(js|css) being removed?

This comment has been minimized.

Copy link
@DouweM

DouweM Feb 6, 2013

Also, am I right that with the defaults of assets.compress = false and assets.debug = true in development, there is no way to have the error pages end up on S3 with properly processed, compressed and concatenated assets?

This comment has been minimized.

Copy link
@epetre

epetre Feb 7, 2013

Author Collaborator

No, this is only for the preview rendering.
When we save the error pages, we do.

File.open(Rails.root.to_s + path, 'w') { |f| f.write(new_content) } 

So all your assets should end up on S3

This comment has been minimized.

Copy link
@DouweM

DouweM Feb 7, 2013

Yeah, in the after_filter of the controller used for previewing. So, in when previewing in development env, the HTML that's written away is the HTML as generated in development env, so unprocessed, uncompressed and unconcatenated. Or am I missing something?

This comment has been minimized.

Copy link
@epetre

epetre Feb 7, 2013

Author Collaborator

Yes, but then... when heroku deploys, after the assets:precompile, we then process those files and ask Sprokets about any assets that are linked in it.

So if you specify any configuration in production, they will be used when we process those assets.

This comment has been minimized.

Copy link
@DouweM

DouweM Feb 7, 2013

Ah! Sorry, I hadn't actually tested it yet, I was just looking through the code. Thanks.

I still don't understand this, though:

What's happening here? Why are all asset references except to /assets/application.(js|css) being removed?

This comment has been minimized.

Copy link
@epetre

epetre Feb 7, 2013

Author Collaborator

Because in production, they are the only ones needed.
All the css you require should go in the application.css
and same goes for the js.

based on
http://guides.rubyonrails.org/asset_pipeline.html

This comment has been minimized.

Copy link
@DouweM

DouweM Feb 7, 2013

Well, I don't use application.(js|css) at all; it's the Rails asset pipeline default, but it's not the only way to go.

Our app is split into a couple of different sections that have no shared CSS, and I'm using <%= stylesheet_link_tag "errors" %> rather than "application" for the error styling.

As a quick public example, GitHub uses https://a248.e.akamai.net/assets.github.com/assets/github-7bc5a6f32e2aa01d928235256cb8f50935425ec5.css as its main CSS file, not assets/application.css. They're using <%= stylesheet_link_tag "github" %>.

So, the final stylesheet name shouldn't be hard-coded, it should be derived from the stylesheet_link_tag and javascript_include_tag calls.

Also, note that <%= stylesheet_link_tag "errors" %> can also result in assets/stylesheets/errors/index.css to be linked rather than errors.css if the latter file doesn't exist, while in the final production version the link would have to be to that processed errors.css.

This comment has been minimized.

Copy link
@epetre

epetre Feb 7, 2013

Author Collaborator

You are right, we are not covering this case right now.
In the short term, we could add a configuration to prevent removal of those files.

It's a good idea to evaluate stylesheet_link_tag and javascript_include_tag of your chosen layout, we will definitely look into that.

This comment has been minimized.

Copy link
@DouweM

DouweM Feb 7, 2013

I'll probably look into it myself as well. I'll submit a pull request once I figure it out.

This comment has been minimized.

Copy link
@epetre

epetre Feb 7, 2013

Author Collaborator

That's great :)


def remove_nodes doc
self.class.nodes_to_remove.each do |selector, predicate|
nodes = doc.search(selector)
i = 0;
unless predicate.nil?
nodes = nodes.select { |n| r = predicate.call(n, i); i +=1;r }
end

nodes.each{|n| n.remove }
end
doc
Expand All @@ -62,24 +72,32 @@ def xslt
@xslt ||= Nokogiri::XSLT(File.open("#{File.dirname(__FILE__)}/pretty-print.xslt"))
end

def new_content
def asset_pipeline_debug_mode?
Object.const_defined?(:Rails) &&
Rails.respond_to?(:application) &&
Rails.application.config.assets.enabled &&
Rails.application.config.assets.debug
end

def new_content params={:keep_default_nodes => false}
html = Nokogiri::HTML(response.body)

edit_nodes html
remove_default_nodes html unless params[:keep_default_nodes]
remove_nodes html

html = xslt.apply_to(html).to_s
gsub_html(html)
end
def render_content
response.body = new_content

def render_content
response.body = new_content(:keep_default_nodes => asset_pipeline_debug_mode?)
to_write = self.class.mappings[params[:action].to_sym]
unless to_write.nil?
path = "/public/#{to_write}.html"
File.open(Rails.root.to_s + path, 'w') { |f| f.write(response.body) }
File.open(Rails.root.to_s + path, 'w') { |f| f.write(new_content) }
end
end
end
end
end
end
14 changes: 14 additions & 0 deletions lib/trackman/tasks/trackman.rake
Expand Up @@ -13,3 +13,17 @@ namespace :trackman do
Trackman::Utility::Configuration.new(:app => args[:app]).setup
end
end

if Rake::Task.task_defined?("assets:precompile:nondigest")
Rake::Task["assets:precompile:nondigest"].enhance do
STDOUT.puts "Trackman: autosyncing..."
Trackman::Assets::Asset.autosync
STDOUT.puts "Trackman: done."
end
elsif Rake::Task.task_defined?("assets:precompile")
Rake::Task["assets:precompile"].enhance do
STDOUT.puts "Trackman: autosyncing..."
Trackman::Assets::Asset.autosync
STDOUT.puts "Trackman: done."
end
end
1 change: 0 additions & 1 deletion lib/trackman/utility/configuration.rb
Expand Up @@ -20,7 +20,6 @@ def setup
raise Trackman::Errors::ConfigSetupError, "Your heroku version is too low, trackman requires '~> 2.26'." unless is_heroku_valid
rename_configs
add_configs
puts "Done!"
end

def get_version
Expand Down
4 changes: 2 additions & 2 deletions lib/trackman/utility/railtie.rb
Expand Up @@ -4,7 +4,7 @@
if ::Rails::VERSION::STRING =~ /^2\.[1-9]/
require './config/environment'
Rails.configuration.middleware.use Trackman::Middleware
elsif ::Rails::VERSION::STRING =~ /^[3-9]\.[0-9]/
elsif ::Rails::VERSION::STRING =~ /^3\.[1-9]/ || ::Rails::VERSION::STRING =~ /^[4-9]\.[0-9]/
require 'generators/controller/controller_generator'
module Trackman
class Railtie < Rails::Railtie
Expand All @@ -20,4 +20,4 @@ class Railtie < Rails::Railtie
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/trackman/version.rb
@@ -1,3 +1,3 @@
module Trackman
VERSION = "0.6.12"
VERSION = "0.6.13"
end
Expand Up @@ -41,4 +41,4 @@ def route_doc show_as_comments = false

doc
end
end
end
14 changes: 14 additions & 0 deletions rails_generators/trackman_tasks/templates/trackman.rake
Expand Up @@ -12,3 +12,17 @@ namespace :trackman do
Trackman::Utility::Configuration.new(:app => args[:app]).setup
end
end

if Rake::Task.task_defined?("assets:precompile:nondigest")
Rake::Task["assets:precompile:nondigest"].enhance do
STDOUT.puts "Trackman: autosyncing..."
Trackman::Assets::Asset.autosync
STDOUT.puts "Trackman: done."
end
elsif Rake::Task.task_defined?("assets:precompile")
Rake::Task["assets:precompile"].enhance do
STDOUT.puts "Trackman: autosyncing..."
Trackman::Assets::Asset.autosync
STDOUT.puts "Trackman: done."
end
end
2 changes: 1 addition & 1 deletion spec/helpers/app_creator.rb
Expand Up @@ -36,4 +36,4 @@ def self.reset
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/scaffold_spec.rb
Expand Up @@ -152,4 +152,4 @@ def self.clean
html.should include('bobby')
html.should include('johnny')
end
end
end
Empty file added spec/test_data/application.css
Empty file.
6 changes: 6 additions & 0 deletions spec/test_data/sample_with_link.html
@@ -0,0 +1,6 @@
<html>
<head>
<link data-turbolinks-track="true" href="application.css" media="all" rel="stylesheet" type="text/css">

</head>
</html>

0 comments on commit a8fac58

Please sign in to comment.