Adjustable paperclip path - ruby

In my on Rails project I have this setup for Paperclip gem, using the DB extension:
# Image attachment and validations
has_attached_file :image,
:storage => :database,
:url => "/:rails_root/public/system/projects/000/000/:id/original/:filename",
:path => "/:rails_root/public/system/projects/000/000/:id/original/:filename"
Now, the image gets this route:
/C:/Users...
If I get rid of the initial / I get images/C:/Users.... Any idea how to get just the rails_root without anything in front?

I found my solution here config/initializers/paperclip.rb
Paperclip::Attachment.default_options.merge!({
:storage => :database,
:url => ":rails_root/public",
:path => ":rails_root/public"
})

Related

Ruby Cucumber env.rb refactor

I have this env.rb file created with a lot of information in it. I like to refactor it to be more readable. Maybe extract some of the code into a separate file or move some of the code to its own class or module file. But I am not sure how to do that.
env.rb
require 'page-object'
require 'page-object/page_factory'
require 'active_record'
World(PageObject::PageFactory)
current_directory = File.dirname __FILE__
web_config_file = current_directory + '/../../config/config.yml'
web_config = YAML.load_file web_config_file
BASE_URL = web_config['testApplicationBaseURL']
browser = Selenium::WebDriver.for :firefox
# Creating two cookies to the browser to avoid a dialog to appear
browser.navigate.to(BASE_URL)
visited_before_cookie = {
:name => 'visitedBefore',
:value => 'yes',
:path => '/',
:domain => 'xxxx.net',
:secure => false
}
saw_browser_suggestion_cookie = {
:name => 'sawBrowserSuggestion',
:value => 'yes',
:path => '/',
:domain => 'xxxx.net',
:secure => false
}
browser.manage.add_cookie(visited_before_cookie)
browser.manage.add_cookie(saw_browser_suggestion_cookie)
#hooks
Before do
#browser = browser
end
at_exit do
browser.close
end
I tried to move the cookie creation piece below to a separate file, but "browser" variable became undefined. I don't know how to scope it.
browser = Selenium::WebDriver.for :firefox
# Creating two cookies to the browser to avoid a dialog to appear
browser.navigate.to(BASE_URL)
visited_before_cookie = {
:name => 'visitedBefore',
:value => 'yes',
:path => '/',
:domain => 'xxxx.net',
:secure => false
}
saw_browser_suggestion_cookie = {
:name => 'sawBrowserSuggestion',
:value => 'yes',
:path => '/',
:domain => 'xxxx.net',
:secure => false
}
browser.manage.add_cookie(visited_before_cookie)
browser.manage.add_cookie(saw_browser_suggestion_cookie)
I thought about moving the hooks to hooks.rb file, but then local variable browser will not be set with those cookies that need to be created.
#hooks
Before do
#browser = browser
end
at_exit do
browser.close
end
How would you do it? Would you use a file or class or module? Please share your solution.
If you have browser initially defined in a top-level block the way you would in env.rb, then you need to pass it in as an argument to code that's defined in other files (i.e. run from other scripts).
So you can, for example (and not necessarily a great example), create a module like
module BrowserConfigurator
visited_before_cookie = {
:name => 'visitedBefore',
:value => 'yes',
:path => '/',
:domain => 'xxxx.net',
:secure => false
}
saw_browser_suggestion_cookie = {
:name => 'sawBrowserSuggestion',
:value => 'yes',
:path => '/',
:domain => 'xxxx.net',
:secure => false
}
def self.create_required_cookies(browser)
browser.manage.add_cookie(visited_before_cookie)
browser.manage.add_cookie(saw_browser_suggestion_cookie)
end
end
and then inside env.rb, call
BrowserConfigurator.create_required_cookies(browser)
(disclaimer: I typed this code here without testing it so it might need tweaking)
Also, the two hooks you wanted to move to hooks.rb cannot be moved precisely because of scoping. Other hooks, on the other hand, can typically be put into a separate file.

Using pony gem and ckeditor in my rails 3 application to send bulk mailing

I am using ckeditor im my application for bulk mailing using pony gem mail is getting sent but I have a couple of problems:
Some tags are also delivered in mail.
If I apply effects to text like bold, italic, underline etc are visible in mail
Image is not visible instead of that {"body"=>" is visible.
Following is the code...
My pony mail function...
params[:l].each do |single_email|
p single_email
Pony.mail(:to => single_email, :from => 'example#example.co.in', :subject => #bmail.subject,
:headers => { "Content-Type" => "text/html"}, :body => #bmail.body, :via => :smtp, :via_options => {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'example.co.in',
:user_name => 'example#example.co.in',
:password => 'paswd',
:authentication => 'plain',
:enable_starttls_auto => true
}) end
Call to ckeditor
%br = cktext_area :body, :body, :ckeditor => {:uiColor => "#AADC6E", :toolbar => "mini"}
%br
Please help me to deliver perfect mail with image and stuffs, plz help me as soon as possible.
Thanz in advance.
try :html_body => #bmail.body
http://rubydoc.info/gems/pony/1.4.1/frames

RSpec for routes and testing routes.rb specifically

I'm trying to write test for my costume routes, along with non existing routes.
For the examples down here I have two problems:
It matches the actions that don't exist on my controller, so it doesn't really match to see if my controller have those actions, it just matches the syntax
the should_not be_routable doesn't work, which kind of goes back to the above problem of it doesn't check against my controller or the routes.rb file to see if the route should exist.
This one passes and is all good:
it "should route to method1" do
{ :get => '/my_controller/method1' }.should route_to(:controller => 'my_controller',
:action => 'method1')
end
this one fails, so it doesn't even check to see if I have a method zz defined in my controller or routes file.
it "should not route to zz" do
{ :get => '/my_controller/zz' }.should_not be_routable
end
the error I get is:
MyController routing zz should route to rescan
Failure/Error: { :get => '/my_controller/zz' }.should_not be_routable
expected {:get=>"/client_rescan/zz"} not to be routable, but it routes to {:controller=>"my_controller", :action=>"zz"}
So it obviously doesn't look at my routes file at all...
This is another example of it doesn't look at my routes file, in my routes I have ' resources :client_rescan, :only => [:index] ' and when I do rake routes it doesn't show delete as expected, but the test doesn't look at those:
it "should not have route to delete" do
{ :delete => '/my_controller/1'}.should_not be_routable
end
The Failure I get is the following. Which looks like it doesn't even see the delete function:
Failure/Error: { :delete => '/my_controller/1'}.should_not be_routable
expected {:delete=>"/my_controller/1"} not to be routable, but it routes to {:controller=>"my_controller", :action=>"1"}
Another problem is my post routes to index in the test:
it "should not have route to post" do
{ :post => '/my_controller' }.should_not be_routable
end
The Failure I get is:
Failure/Error: { :post => '/my_controller' }.should_not be_routable
expected {:post=>"/my_controller"} not to be routable, but it routes to {:controller=>"my_controller", :action=>"index"}
this is content of my routes.rb file
require 'resque/server'
require 'resque_scheduler'
Cdc::Application.routes.draw do
mount Resque::Server.new, :at => 'resque'
Resque.schedule = YAML.load_file(File.join(File.dirname(__FILE__), 'resque_schedule.yml')) # load the schedule
devise_for :users, :controllers => { :sessions => "sessions", :registrations => "registrations" }
[:assessments, :security_assessments, :privacy_assessments].each do |assessment_type|
resources assessment_type, :controller => :assessments do
resources :rsz do
post 'review', :on => :member
get 'judgement', :on => :member
end
get :judgement, :on => :member
resources :samples do
get 'download', :on => :member
end
resources :asm_reviews do
post :request_review, :on => :collection
end
end
end
resources :account_creator
match "/images/captcha/*file_name" => "captcha#show"
## this is where my route is!! :)
resources :my_controller, :only => [:index] do
collection do
get :rescan
get :cancel
end
end
match "alert_queue/words" => "alert_queue#words"
resources :alert_queue
match "calls_to_action/start/:id" => "calls_to_action#start", :id => /\d+/
match "calls_to_action/close/:id" => "calls_to_action#close", :id => /\d+/
match "calls_to_action/comment/:id" => "calls_to_action#comment", :id => /\d+/
match "calls_to_action/multiclose" => "calls_to_action#multiclose"
match "calls_to_action/multiscan" => "calls_to_action#multiscan"
match "application_instances/multiclose" => "application_instances#multiclose"
match "application_instances/multiscan" => "application_instances#multiscan"
resources :code_categories do
resources :code_families do
resources :code_family_versions
end
end
resources :code_policy_items
resources :application_instances do
collection do
post :resque_statuses
end
resources :code_indices
resources :application_analysis
get "smali/:smali_path", :as => :smali, :on => :member, :action => :smali, :smali_path => /.*/
member do
get :file
get :strings
get :dump
get :alerts
get :correlations
get :signers
get :icon
get :resque_status
get :assets
get :new_code_index
get :class_list
get :heuristic_hits
get :engine_artifacts
post :rescan
post :attach_artifact
post :engine_artifact, :action => :attach_engine_artifact
resources :alerts
end
post "multiscan", :on => :collection, :action => :multiscan
post "multiclose", :on => :collection, :action=> :multiclose
post "engines/:engine_name/:engine_version/assertions/:cookie",
:on => :member, :action => :register_assertion_set, :as => :register_assertion_set,
:engine_name => /[^\/]+/, :engine_version => /[^\/]+/, :cookie => /[^\/]+/
post "engines/:engine_name/:engine_version/network_data",
:on => :member, :action => :register_network_data, :as => :register_network_data,
:engine_name => /[^\/]+/, :engine_version => /[^\/]+/
post "assets", :on => :member, :action => :register_asset_set, :as => :register_asset_set
end
# index gets the list of families, show gets the assertion types for that family
resources :assertion_families
resources :artifacts
resources :dashboard do
collection do
get :last_app_instance
end
end
match '/direct_downloads/' => 'direct_downloads#index'
root :to => "stats#index"
match '/' => 'stats#index'
match 'explorer/query/:format' => 'explorer#query'
match '/:controller(/:action(/:id))'
end
The problem is this line of routes.rb:
match '/:controller(/:action(/:id))'
This is a catch all route, and will match for example /abc/xyz to controller abc, and action xyz.
It's best really not to use catch all routes, in favour of exposing only the functionality you want.

Devise route not respected on Devise view pages

I have implemented devise and set up the routes to prettyify the urls as so (in the routes file):
devise_scope :user do
get "/login" => "devise/sessions#new"
get "/logout" => "devise/sessions#destroy"
get "/register" => "devise/registrations#new"
end
I have global menu in my application.html.erb file, however, which now inserts devise/controller/action on every link that is on the login or register pages, such as
<%= link_to "Upload Video", {:controller => "videos", :action => "new"} %>
becomes devise/videos/new
Any ideas on how to fix this? I can hack around it but I'm pretty sure it is a simple fix.
Cheers,
s
Take out the devise_scope and try something like this:
devise_for :users,
:controllers => {:registrations => 'devise/registrations', :sessions => 'devise/sessions'},
:path => '/',
:path_names => {:sign_in => 'login', :sign_out => 'logout'}

Got S3::Error::SignatureDoesNotMatch by using Paperclip with "s3" on certain path configuration

Hi I got the below in certain path configuration:
S3::Error::SignatureDoesNotMatch (The request signature we calculated does not match the signature you provided. Check your key and signing method.):
config/initializers/paperclip_2_s3.rb:124:in `block in flush_writes'
config/initializers/paperclip_2_s3.rb:113:in `each'
config/initializers/paperclip_2_s3.rb:113:in `flush_writes'
app/controllers/admin/images_controller.rb:12:in `create'
has_mongoid_attached_file :photo,
:styles => { :thumb => "120x120#" },
:convert_options => { :all => "-quality 92 +profile '!icc,*'" },
:storage => :s3,
# :path => '/:class/:attachment/:style/:basename.:extension',
:path => ':class/:attachment/:id_partition/:basename.:style.:extension',
:url => ':s3_domain_url',
:s3_credentials => {
:access_key_id => ApplicationController.aws_access_key,
:secret_access_key => ApplicationController.aws_secret_access_key
},
:bucket => proc { |attachment| if attachment.instance.imagable.respond_to? (:domain) then ApplicationController.bucket_name(attachment.instance.imagable.domain) else ApplicationController.bucket_name(attachment.instance.imagable.site.domain) end }
If I change the configuration to this one:
has_mongoid_attached_file :photo,
:styles => { :thumb => "120x120#" },
:convert_options => { :all => "-quality 92 +profile '!icc,*'" },
:storage => :s3,
# :path => '/:class/:attachment/:style/:basename.:extension',
:path => ':class/:attachment/:id_partition/:basename.:style.:extension',
:url => ':s3_domain_url',
:s3_credentials => {
:access_key_id => ApplicationController.aws_access_key,
:secret_access_key => ApplicationController.aws_secret_access_key
},
:bucket => proc { |attachment| if attachment.instance.imagable.respond_to? (:domain) then ApplicationController.bucket_name(attachment.instance.imagable.domain) else ApplicationController.bucket_name(attachment.instance.imagable.site.domain) end }
Then everything is ok....
So I changed only the storage path
# :path => '/:class/:attachment/:style/:basename.:extension',
:path => ':class/:attachment/:id_partition/:basename.:style.:extension',
This works also:
:path => ':class/:attachment/:id_partition/:basename:style.:extension',
Can anybody explain this?
Another interesting thing:
:path => '/:id_partition/:basename.:extension',
in this case I also got this:
URI::InvalidURIError (the scheme does not accept registry part: 4e5e (or bad hostname?)):
Any explanations are welcome... :)
After inspecting the code of the S3 code, it reveals that the :id or the partitioned version :id_partition must be a necessary thing in the path.

Resources