render static assets + fix small and various bugs when rendering a page
did
committed Feb 15, 2015
commit b9d79d3e7576a3d419d61b1687dd9c92978177f4
Showing 13
changed files with
88 additions
and 54 deletions
example/server.rb
+2
-0
| @@ | @@ -14,6 +14,8 @@ path = ENV['SITE_PATH'] || File.join(File.expand_path(File.dirname(__FILE__)), ' |
| Locomotive::Steam.configure do |config| | |
| config.mode = :test | |
| + | config.serve_assets = true |
| + | config.assets_path = File.join(path, 'public') |
| end | |
| Locomotive::Common.reset | |
locomotive/steam/configuration.rb b/lib/locomotive/steam/configuration.rb
+3
-0
| @@ | @@ -10,6 +10,7 @@ module Locomotive |
| attr_accessor :theme_assets_checksum | |
| attr_accessor :asset_host | |
| + | attr_accessor :serve_assets |
| attr_accessor :assets_path | |
| attr_accessor :image_resizer_secret | |
| @@ | @@ -19,6 +20,8 @@ module Locomotive |
| self.mode = :production | |
| self.theme_assets_checksum = false | |
| + | self.serve_assets = false |
| + | |
| self.image_resizer_secret = 'please change it' | |
| self.csrf_protection = true | |
locomotive/steam/decorators/template_decorator.rb b/lib/locomotive/steam/decorators/template_decorator.rb
+17
-7
| @@ | @@ -6,18 +6,28 @@ module Locomotive |
| def liquid_source | |
| if attributes.key?(:template_path) | |
| - | source = File.open(template_path).read.force_encoding('utf-8') |
| - | |
| - | if match = source.match(/^((---\s*\n.*?\n?)^(---\s*$\n?))?(?<template>.*)/m) |
| - | match[:template] |
| - | else |
| - | source |
| - | end |
| + | source_from_template_file |
| else | |
| self.source | |
| end | |
| end | |
| + | private |
| + | |
| + | def source_from_template_file |
| + | source = File.open(template_path).read.force_encoding('utf-8') |
| + | |
| + | if match = source.match(/^((---\s*\n.*?\n?)^(---\s*$\n?))?(?<template>.*)/m) |
| + | source = match[:template] |
| + | end |
| + | |
| + | if template_path.ends_with?('.haml') |
| + | Haml::Engine.new(source).render |
| + | else |
| + | source |
| + | end |
| + | end |
| + | |
| end | |
| end | |
locomotive/steam/middlewares.rb b/lib/locomotive/steam/middlewares.rb
+2
-2
| @@ | @@ -10,8 +10,8 @@ require_relative 'middlewares/logging' |
| require_relative 'middlewares/path' | |
| require_relative 'middlewares/page' | |
| - | # require_relative 'middlewares/static_assets' |
| - | # require_relative 'middlewares/dynamic_assets' |
| + | require_relative 'middlewares/static_assets' |
| + | require_relative 'middlewares/dynamic_assets' |
| # require_relative 'middlewares/entry_submission' | |
locomotive/steam/middlewares/dynamic_assets.rb b/lib/locomotive/steam/middlewares/dynamic_assets.rb
+13
-13
| @@ | @@ -3,32 +3,32 @@ require 'coffee_script' |
| module Locomotive::Steam | |
| module Middlewares | |
| - | class DynamicAssets < Base |
| + | class DynamicAssets # < Base |
| attr_reader :app, :regexp | |
| def initialize(app) | |
| - | super(app) |
| - | |
| - | @regexp = /^\/(javascripts|stylesheets)\/(.*)$/ |
| + | @regexp = /^\/(javascripts|stylesheets)\/(.*)$/o |
| end | |
| def call(env) | |
| - | dup._call(env) # thread-safe purpose |
| + | app.call(env) |
| + | # _call(env) |
| + | # dup._call(env) # thread-safe purpose |
| end | |
| def _call(env) | |
| if env['PATH_INFO'] =~ self.regexp | |
| env['PATH_INFO'] = $2 | |
| - | base_path = env['steam.mounting_point'].path |
| + | # base_path = env['steam.mounting_point'].path |
| - | begin |
| - | sprockets = Locomotive::Mounter::Extensions::Sprockets.environment(base_path) |
| - | sprockets.call(env) |
| - | rescue Exception => e |
| - | raise Locomotive::Steam::DefaultException.new "Unable to serve a dynamic asset. Please check the logs.", e |
| - | end |
| + | # begin |
| + | # sprockets = Locomotive::Mounter::Extensions::Sprockets.environment(base_path) |
| + | # sprockets.call(env) |
| + | # rescue Exception => e |
| + | # raise Locomotive::Steam::DefaultException.new "Unable to serve a dynamic asset. Please check the logs.", e |
| + | # end |
| else | |
| app.call(env) | |
| end | |
| @@ | @@ -37,4 +37,4 @@ module Locomotive::Steam |
| end | |
| end | |
| - | end |
| \ No newline at end of file | |
| + | end |
locomotive/steam/middlewares/static_assets.rb b/lib/locomotive/steam/middlewares/static_assets.rb
+16
-16
| @@ | @@ -1,25 +1,25 @@ |
| - | require 'rack/static' |
| + | # require 'rack/static' |
| - | module Locomotive::Steam |
| - | module Middlewares |
| + | # module Locomotive::Steam |
| + | # module Middlewares |
| - | class StaticAssets < ::Rack::Static |
| + | # class StaticAssets < ::Rack::Static |
| - | alias_method :call_without_threadsafety, :call |
| + | # alias_method :call_without_threadsafety, :call |
| - | def call(env) |
| - | dup._call(env) # thread-safe purpose |
| - | end |
| + | # def call(env) |
| + | # dup._call(env) # thread-safe purpose |
| + | # end |
| - | def _call(env) |
| - | mounting_point = env['steam.mounting_point'] |
| + | # def _call(env) |
| + | # # mounting_point = env['steam.mounting_point'] |
| - | @file_server = Rack::File.new(mounting_point.assets_path) |
| + | # @file_server = Rack::File.new(mounting_point.assets_path) |
| - | call_without_threadsafety(env) |
| - | end |
| + | # call_without_threadsafety(env) |
| + | # end |
| - | end |
| + | # end |
| - | end |
| - | end |
| \ No newline at end of file | |
| + | # end |
| + | # end |
locomotive/steam/repositories/filesystem/models/content_entry.rb b/lib/locomotive/steam/repositories/filesystem/models/content_entry.rb
+4
-0
| @@ | @@ -47,6 +47,10 @@ module Locomotive |
| self.class.localized_attributes + content_type.localized_fields_names | |
| end | |
| + | def to_liquid |
| + | Locomotive::Steam::Liquid::Drops::ContentEntry.new(self) |
| + | end |
| + | |
| private | |
| def is_dynamic_attribute?(name) | |
locomotive/steam/repositories/filesystem/models/site.rb b/lib/locomotive/steam/repositories/filesystem/models/site.rb
+4
-0
| @@ | @@ -10,6 +10,10 @@ module Locomotive |
| attr_accessor :root_path | |
| + | def initialize(attributes = {}) |
| + | super({ timezone: 'UTC' }.merge(attributes)) |
| + | end |
| + | |
| def default_locale | |
| self.locales.try(:first) || :en | |
| end | |
locomotive/steam/repositories/filesystem/sanitizers/content_type.rb b/lib/locomotive/steam/repositories/filesystem/sanitizers/content_type.rb
+1
-1
| @@ | @@ -26,7 +26,7 @@ module Locomotive |
| _attributes[:label] = name.to_s.humanize | |
| end | |
| - | _attributes[:type] = _attributes[:type].to_sym |
| + | _attributes[:type] = _attributes[:type].try(:to_sym) |
| Filesystem::Models::ContentTypeField.new(_attributes) | |
| end | |
locomotive/steam/server.rb b/lib/locomotive/steam/server.rb
+8
-0
| @@ | @@ -15,6 +15,14 @@ module Locomotive::Steam |
| Rack::Builder.new do | |
| use Rack::Lint | |
| + | if server.options[:serve_assets] |
| + | use ::Rack::Static, { |
| + | root: Locomotive::Steam.configuration.assets_path, |
| + | urls: ['/images', '/fonts', '/samples', '/media'] |
| + | } |
| + | # use Middlewares::DynamicAssets # TODO |
| + | end |
| + | |
| use Middlewares::Favicon | |
| use Middlewares::DefaultEnv, server.options | |
spec/integration/server/basic_spec.rb
+15
-13
| @@ | @@ -8,22 +8,24 @@ describe Locomotive::Steam::Server do |
| run_server | |
| end | |
| - | # it 'can render the index page' do |
| - | # get '/index' |
| - | # expect(last_response.status).to eq(200) |
| - | # end |
| - | |
| - | # it 'shows the index page' do |
| - | # get '/index' |
| - | # expect(last_response.body).to match(/Upcoming events/) |
| - | # end |
| + | it 'shows the index page' do |
| + | get '/index' |
| + | expect(last_response.status).to eq(200) |
| + | expect(last_response.body).to match(/Upcoming events/) |
| + | end |
| - | it 'shows the 404 page' do |
| - | get '/void' |
| - | expect(last_response.status).to eq(404) |
| - | expect(last_response.body).to match /page not found/ |
| + | it 'renders an image' do |
| + | get '/images/nav_on.png' |
| + | expect(last_response.status).to eq(200) |
| end | |
| + | # it 'shows the 404 page' do |
| + | # get '/void' |
| + | # expect(last_response.status).to eq(404) |
| + | # expect(last_response.body).to match /page not found/ |
| + | # puts last_response.body.inspect |
| + | # end |
| + | |
| # it 'shows the 404 page with 200 status code when its called explicitly', pending: true do | |
| # get '/404' | |
| # last_response.status.should eq(200) | |
spec/spec_helper.rb
+2
-1
| @@ | @@ -29,7 +29,8 @@ require_relative '../lib/locomotive/steam/repositories/filesystem' |
| require_relative 'support' | |
| Locomotive::Steam.configure do |config| | |
| - | config.mode = :test |
| + | config.mode = :test |
| + | config.assets_path = File.join(File.expand_path(File.dirname(__FILE__)), 'fixtures', 'default', 'public') |
| end | |
| RSpec.configure do |config| | |
spec/support/helpers.rb
+1
-1
| @@ | @@ -22,7 +22,7 @@ module Spec |
| setup_common #(File.join(default_fixture_site_path, 'log/steam.log')) | |
| Locomotive::Common::Logger.info 'Server started...' | |
| - | Locomotive::Steam::Server.new(path: default_fixture_site_path).to_app |
| + | Locomotive::Steam::Server.new(path: default_fixture_site_path, serve_assets: true).to_app |
| end | |
| def default_fixture_site_path | |