How to set x frame default options in rails 3 - ruby-on-rails

In rails 4 this could be done under the config file if it already wasn't default.
config.action_dispatch.default_headers = { 'X-Frame-Options' => 'SAMEORIGIN' }
Is there a way to set the headers like that in rails 3.2.22.4? Thanks!

See these discussions:
https://github.com/rails/rails/issues/6311
https://github.com/rails/rails/pull/7302
default_headers configuration option (and 'SAMEORIGIN' on by default) was introduced in version 4.0. Before that headers can be manipulated with custom middlewares, or individually for each action as:
headers['X-Frame-Options'] = 'SAMEORIGIN'

Related

Ruby on rails - pass params from db to environment config file

im using a gem for a sms service, and i have to put the params in the specific environment config file.
All is ok with static params, but if i call this params from a db, starting the webrick i have an error:
(ActiveRecord::ConnectionNotEstablished)
this is my original code in development.rb
Skuby.setup do |config|
config.method = 'send_sms_classic' #default
config.username = 'myusername'
config.password = 'mypassword'
config.password = Setting.where(campo: 'skebby_password').valore
config.sender_string = 'company' #optional
config.sender_number = '39123456790' #optional
config.charset = 'UTF-8' #skebby default is ISO-8859-1
end
changing something, like:
config.password = Setting.where(campo: 'skebby_password').valore
it goes down!
How can i fix?
Do you think it's ok to do this in your config/environment. I would think that such would be better suited for your config/initializers, within your initializer, your db connection is already setup.
BTW, if you're using different parameters for your environments, I think you should rather use a configuration file, which would set your params per environment and you could then use that.

Set System Directory Rails Production Environment

I have an app that works fine in on my development machine, but on my production server it uses a broken link to serve an image served using the Paperclip Gem.
Production environment is Linux(Debian), Apache, Passenger and I am deploying with Capistrano.
The app is stored in (a symlink that points to the public folder of the current version of the app deployed using capistrano):
/var/www/apps/root/appname
However, when I try and access it on the production server, the Apache error log displays this as the path it is looking in:
/var/www/apps/root/system
The correct path, however, is:
/var/www/apps/appname/shared/system
One option available to me is to create a symlink in root that directs system to the correct path, but I don't want to do this in case I want to deploy another app in the same root dir.
The url for this request is generated by rails, but Apache is what fetches the static resource (image files), so I have tried placing the following in my config/environments/production.rb:
ENV["RAILS_RELATIVE_URL_ROOT"] = '/appname/'
Which has resolved all other pathing issues I've been experiencing, but when rails generates the url (via the Paperclip gem), it doesn't seem to use it.
How can I set it so Paperclip uses the right path and only uses it production?
I've a workaround, add this as one of initializers:
config/initializer/paperclip.rb
Paperclip::Attachment.class_eval do
def url(style_name = default_style, options = {})
if options == true || options == false # Backwards compatibility.
res = #url_generator.for(style_name, default_options.merge(:timestamp => options))
else
res = #url_generator.for(style_name, default_options.merge(options))
end
# replace adding uri before res, minus final /
Rails.application.config.site_relative_url[0..-2]+res
end
end
At the moment Paperclip doesn’t work with ENV['RAILS_RELATIVE_URL_ROOT'] and the. You can follow the issue here:
https://github.com/thoughtbot/paperclip/issues/889

Dynamic CSS using Sprockets

I'm working on a site builder in rails and I would like to render the sites css using Sprockets SCSS processors. Since the user can change colors and logos, I can't use Sprockets precompilation so I've started working on a Rails SCSS template handler to handle dynamic views.
The goal is to compile 'app/views/sites/show.css.scss' any time /sites/43543.css is requested. Here's what I have so far. You'll notice I first run the template through the ERB processor and then attempt to run it through Sprockets.
https://gist.github.com/3870095
Manuel Meurer came up with an alternative solution that writes the ERB output to a path and then triggers the Asset Pipeline to compile it. I was able to get his solution to work locally but it wont work on heroku because the asset path is not writable. Files can only be written to the tmp directory and those files are only guaranteed for a single request.
http://www.krautcomputing.com/blog/2012/03/27/how-to-compile-custom-sass-stylesheets-dynamically-during-runtime/
After a long day I was able to solve my problem thanks to John Feminella and his post on google. The challenging part for me was figuring out how to create a new Sprockets::Context. Luckily John's solution doesn't require a Context.
Updated gist here
Attempt #1
This code does not allow importing css files from the asset pipeline.
#import "foundation"; works because foundation is loaded as a compass module
#import "custom_css"; results in an error message saying the file could not be found
def call(template)
erb = ActionView::Template.registered_template_handler(:erb).call(template)
%{
options = Compass.configuration.to_sass_engine_options.merge(
:syntax => :scss,
:custom => {:resolver => ::Sass::Rails::Resolver.new(CompassRails.context)},
)
Sass::Engine.new((begin;#{erb};end), options).render
}
end
Attempt #2
This code fails to embed base64 urls using asset-data-url
def call(template)
erb = ActionView::Template.registered_template_handler(:erb).call(template)
%{
compiler = Compass::Compiler.new *Compass.configuration.to_compiler_arguments
options = compiler.options.merge({
:syntax => :scss,
:custom => {:resolver => ::Sass::Rails::Resolver.new(CompassRails.context)},
})
Sass::Engine.new((begin;#{erb};end), options).render
}
end
Attempt 3
Turns out you can use empty values while creating the context. Code below works in development but throws an error in production.
ActionView::Template::Error (can't modify immutable index)
It appears the error occurs in Sprockets::Index which is used instead of Sprockets::Environment in production. Switching to Sprockets::Environment doesn't solve the problem either.
def call(template)
erb = ActionView::Template.registered_template_handler(:erb).call(template)
%{
context = CompassRails.context.new(::Rails.application.assets, '', Pathname.new(''))
resolver = ::Sass::Rails::Resolver.new(context)
compiler = Compass::Compiler.new *Compass.configuration.to_compiler_arguments
options = compiler.options.merge({
:syntax => :scss,
:custom => {:resolver => resolver}
})
Sass::Engine.new((begin;#{erb};end), options).render
}
end

Rails 3 and PDFKit. How to specify page size?

I have been looking in the documentation but can't find the answer. How can I specify the page size of my pdf document and what are the available page sizes? I keep on looking and looking but I can't find good documentation. Please point me to a URL or let me know how can I code some page size into my PDF document.
Oh and I don't want to do that on any config file because I need to generate PDf documents of different sizes.
NOT in config file...
PDFKit.configure do |config|
config.wkhtmltopdf = `which wkhtmltopdf`.to_s.strip
config.default_options = {
:encoding=>"UTF-8",
:page_size=>"A4", #or "Letter" or whatever needed
:margin_top=>"0.25in",
:margin_right=>"1in",
:margin_bottom=>"0.25in",
:margin_left=>"1in",
:disable_smart_shrinking=>false
}
end
You can set the page size when creating a new PDF like this:
kit = PDFKit.new(source, :page_size => "Legal")
PDFKit uses WKHTMLTOPDF which in turn uses QPrinter. You can find the available sizes in the QPrinter documentation (there's a bunch), but its pretty safe to say that any size paper you want is available. Also, you can set a custom size if you can't find what you need.
NB: If you don't set a default option for page_size in a config somewhere AND don't supply one in your method call, PDFKit will use its internal default (Letter). See line 10 of lib/pdfkit/configuration.rb
Pdkit accepts custom sizes:
PDFKit.configure do |config|
config.wkhtmltopdf = `which wkhtmltopdf`.strip
config.default_options = {
:page_width => '1682',
:page_height => '2378'
}
end
The sizes must be in milimeters (wkthmltopdf documentation).
Since it's using wkhtmltopdf to generate the PDFs I'm assuming you can use the same options that it supports. In a wkhtmltopdf manual I found, it mentions the following site for a list of sizes:
http://doc.trolltech.com/4.6/qprinter.html#PaperSize-enum
To set the page size, you can use the :page_size option like so:
PDFKit.new(html, :page_size => 'Letter')

Redcarpet & Middleman: :with_toc_data

I'm wondering about using Redcarpet's :with_toc_data option for Markdown working with Middleman (a Sinatra-based static site generator).
Our current config.rb:
set :markdown, :layout_engine => :haml
set :markdown_engine, :redcarpet
This doesn't work:
set :markdown, :layout_engine => :haml, :with_toc_data => true
set :markdown_engine, :redcarpet
Any help is very appreciated!
Fixed in Middleman 3.0 by Thomas Reynolds: https://github.com/middleman/middleman/issues/442
It appears that from Issue #200 of Middleman at Github, this should be done as such:
set :markdown, :layout_engine => :haml
set :markdown_engine, :redcarpet
set :redcarpet, :with_toc_data => true
The third line being the key. I also can't make this work, so there might be something still open as a bug with Middleman?
The latest release is 2.0.15.3, which is what I have installed; but I also can't make it work. Perhaps Issue #200 should be re-opened?
I have this exact code in my config.rb:
###
# GitHib flavoured Markdown, I can't go back!
###
set :markdown_engine, :redcarpet
set :redcarpet, fenced_code_blocks: true, autolink: true
I'd be eager to understand if I am doing something incorrectly. (I'm specifically trying to use this in a Middleman Blog)
Update to my answer: The commit referenced in Issue #200 does not exist in the 2.0.15.3 release, thus we'll have to use something newer.

Resources