I'm working on an Rails 4.1 engine that handles user uploads of photos and videos. I'm using Mongoid-Paperclip to handle the uploads and Paperclip-av-transcoder to encode the videos into several formats. All files are stored at S3. All of that works fine, but as you can expect, encoding the videos can take quite some time, so the next step is to make that happen in the background. I did some googling and found Delayed_Paperclip that seems to do what I need. After that it seemed that Sidekiq was the best option for handling the background processing.
Now the problem is, I can't make all this work together. Running my unit tests I get NoMethodError: undefined method 'process_in_background' so it seems the problem resides on Delayed_Paperclip, although there is no special setup to it.
This is the model firing the problem
module MyEngine
class Video
include Mongoid::Document
include Mongoid::Paperclip
has_mongoid_attached_file :file,
:path => ':hash.:extension',
:hash_secret => "the-secret",
:storage => :s3,
:url => ':s3_domain_url',
:s3_credentials => File.join(Rails.root, 'config', 's3.yml'),
:bucket => "my-bucket-#{Rails.env}",
:styles => {
:mp4 => { :format => 'mp4', :convert_options => { :output => { :vcodec => 'libx264', :acodec => 'copy' } } },
:ogg => { :format => 'ogg', :auto_rotate => true },
:webm => { :format => 'webm', :auto_rotate => true },
:thumb => { :geometry => "250x187#", :format => 'jpg', :time => 10, :auto_rotate => true }
},
:processors => [:transcoder]
validates_attachment :file, :content_type => { :content_type => ["video/x-flv", "video/mp4", "video/ogg", "video/webm", "video/x-ms-wmv", "video/x-msvideo", "video/quicktime", "video/3gpp"] }
process_in_background :file
end
end
I've tried adding require "delayed_paperclip" to the lib/myengine/myengine.rb file but that didn't help.
Regarding Sidekiq, I have added to the test_helper.rb the following:
require 'sidekiq/testing'
Sidekiq::Testing.inline!
Note that I did not forget to run bundle install and Redis is up and running. I'm using Mongoid, not active record.
What I am doing wrong? Has anyone successfully used this setup? Is there another combination of gems that I should try?
Aditional info:
Delayed_paperclip 2.9.1
Mongoid 4.0.2
Mongoid-Paperclip 0.0.9
Paperclip 4.2.1
Paperclip-av-transcoder 0.6.4
Rails 4.1.9
Sidekiq 3.5.0
I've been digging through the code of delayed_paperclip and it is definitely tied to ActiveRecord so not compatible with Mongoid. I gave a try to mongoid_paperclip_queue but that gem hasn't been updated in 4 years and doesn't seem to work with the current versions of rails/mongoid/paperclip as far as I can tell.
I therefore decided the best way to solve my issue would be to override the code of delayed_paperclip that integrates with ActiveRecord and make it instead work with Mongoid.
This is what I ended up doing, and seems to be working fine so far:
lib/myengine.rb
require "mongoid_paperclip"
require "paperclip/av/transcoder"
require "delayed_paperclip"
require "myengine/engine"
module Myengine
end
DelayedPaperclip::Railtie.class_eval do
initializer 'delayed_paperclip.insert_into_mongoid' do |app|
ActiveSupport.on_load :mongoid do
DelayedPaperclip::Railtie.insert
end
if app.config.respond_to?(:delayed_paperclip_defaults)
DelayedPaperclip.options.merge!(app.config.delayed_paperclip_defaults)
end
end
# Attachment and URL Generator extends Paperclip
def self.insert
Paperclip::Attachment.send(:include, DelayedPaperclip::Attachment)
Paperclip::UrlGenerator.send(:include, DelayedPaperclip::UrlGenerator)
end
end
DelayedPaperclip::InstanceMethods.class_eval do
def enqueue_post_processing_for name
DelayedPaperclip.enqueue(self.class.name, read_attribute(:id).to_s, name.to_sym)
end
end
Then all you need is to include the delayed_paperclip glue to the model:
module Myengine
class Video
include Mongoid::Document
include Mongoid::Paperclip
include DelayedPaperclip::Glue # <---- Include this
has_mongoid_attached_file :file,
:path => ':hash.:extension',
:hash_secret => "the-secret",
:storage => :s3,
:url => ':s3_domain_url',
:s3_credentials => File.join(Rails.root, 'config', 's3.yml'),
:bucket => "my-bucket-#{Rails.env}",
:styles => {
:mp4 => { :format => 'mp4', :convert_options => { :output => { :vcodec => 'libx264', :acodec => 'copy' } } },
:ogg => { :format => 'ogg', :auto_rotate => true },
:webm => { :format => 'webm', :auto_rotate => true },
:thumb => { :geometry => "250x187#", :format => 'jpg', :time => 10, :auto_rotate => true }
},
:processors => [:transcoder]
validates_attachment :file, :content_type => { :content_type => ["video/x-flv", "video/mp4", "video/ogg", "video/webm", "video/x-ms-wmv", "video/x-msvideo", "video/quicktime", "video/3gpp"] }
process_in_background :file
end
end
Hopefully this will save somebody else all the trouble.
Related
I am using paperclip with ckeditor and I get this error when i click on "send it to the server" : "data has contents that are not what they are reported to be".
That's what "attachment_file.rb" and "picture.rb" look like:
class Ckeditor::AttachmentFile < Ckeditor::Asset
has_attached_file :data, :styles => { :content => '575>', :thumb => '80x80#' },
:storage => :s3, :s3_credentials => "/config/s3.yml", :path => ":attachment/:id/:style.:extension",
:url => ":s3_domain_url"
validates_attachment_presence :data
validates_attachment_size :data, less_than: 100.megabytes
do_not_validate_attachment_file_type :data
def url_thumb
#url_thumb ||= Ckeditor::Utils.filethumb(filename)
end
end
you should remove the do_not_validate_attachment_file_type :data line from your code, or just change the editor .
I'm using a custom Devise SessionsController:
#custom_sessions_controller.rb
class CustomSessionsController < Devise::SessionsController
end
In my routes.rb devise is set up like this:
#routes.rb
devise_for :custom_users, {
:singular => 'custom_user',
:class_name => 'CustomUser',
:path => "/",
:path_names => { :sign_in => 'login', :sign_out => 'logout' },
:controllers => { :sessions => "custom_sessions" }
}
I would like to write a simple rspec test:
#custom_sessions_controller_spec.rb
require 'rails_helper'
describe CustomSessionsController, :type => :controller do
describe "login" do
before do
setup_controller_for_warden
#request.env["devise.mapping"] = Devise.mappings[:custom_user]
#my_user = FactoryGirl.create(:custom_user) # creates user with valid credentials
end
it "should succeed with valid credentials" do
sign_in #my_user
curr_user = assigns(:current_custom_user)
expect(curr_user).to eq(#my_user)
expect(response).to be_success
end
end
end
In my rails_helper.rb the following lines are present:
require 'devise'
...
RSpec.configure do |config|
...
config.include Devise::TestHelpers, :type => :controller
config.include Warden::Test::Helpers , :type => :controller
...
The problem is that curr_user in the test is alway nil. What am I doing wrong? Or how to test a custom devise session controller? Or how to log in in - other - tests using a custom devise session?
instead of:
describe CustomSessionsController, :type => :controller do
try:
RSpec.describe CustomSessionsController, :type => :controller do
Hope it works!
Using
#controller.current_custom_user
instead of
assigns(:current_custom_user)
seems to work.
I'm running Rails 4.2.3. I have a model, Volunteer, with a serialized attribute, participated. After object creation, I can't update the participated attribute:
> #volunteer = Volunteer.new( participated: {'citizenship' => '', 'daca' => ''} )
=> #<Volunteer id: 1, participated: {'citizenship' => '', 'daca' => ''}>
> #volunteer.participated['citizenship'] = 'test'
=> {'citizenship' => 'test', 'daca' => ''}
> #volunteer
=> #<Volunteer id: 1, participated: {'citizenship' => 'test', 'daca' => ''}>
> #volunteer.changed?
=> true
> #volunteer.save
=> true
> Volunteer.last
=> #<Volunteer id: 1, participated: {'citizenship' => '', 'daca' => ''}>
As you can see, the database isn't being updated. I was originally running rails 4.2.0. At that time, changed? wasn't even returning true--in this scenario, it was returning false. After some searching, this pull request on GitHub led me to suspect that the issue might be a Rails bug. Updating to rails 4.2.3 fixed the problems with changed?, but the database still isn't updating.
Any suggestions would be greatly appreciated! I've sunk several hours into this so far.
For what its worth:
class Volunteer < ActiveRecord::Base
serialize :participated
end
class CreateVolunteers < ActiveRecord::Migration
def change
create_table :volunteers do |t|
t.text :participated
t.timestamps null: false
end
end
end
Hi can anyone point me in the direction of what I am doing wrong. I am trying to upload an image to an S3 bucket in EU region, Ireland, using a rails application in development. This is the error I am getting the error
SocketError in ProductsController
getaddrinfo: nodename nor servname provided, or not known
On the following line : TCPSocket.open(conn_address, conn_port, #local_host, #local_port)
I am using the aws-sdk gem and paperclip 4.2.
Here are some code snippets
In:
config/enviroments/development.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_host_name => "s3-eu-west-1.amazonaws.com",
:s3_credentials => {
:bucket => 'S3_BUCKET_NAME',
:access_key_id => 'AWS_ACCESS_KEY_ID',
:secret_access_key => 'AWS_SECRET_ACCESS_KEY'
}
In
config/aws.yml
development:
AWS_ACCESS_KEY_ID: "xxx"
AWS_SECRET_ACCESS_KEY: "xxx"
S3_BUCKET_NAME: "xxx"
s3_host_name: 's3-eu-west-1.amazonaws.com'
In my model
class Product < ActiveRecord::Base
validates :avatar,
attachment_content_type: { content_type: /\Aimage\/.*\Z/ },
attachment_size: { less_than: 5.megabytes }
has_attached_file :avatar, styles: {
thumb: '100x100>',
square: '200x200#',
medium: '300x300>'
}
end
What am I missing? I have looked at every example I could find online and tried to adjust but with no luck.
Thanks
Try this:
#config/enviroments/development.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_host_name => "s3-eu-west-1.amazonaws.com",
:s3_credentials => {
:bucket => ENV["S3_BUCKET_NAME"],
:access_key_id => ENV["AWS_ACCESS_KEY_ID"],
:secret_access_key => ENV["AWS_SECRET_ACCESS_KEY"]
}
Ok, I've solved this by hardcoding the AWS credentials into the development.rb file. So it looks like the problem was the aws.yml file wasn't being loaded, does anyone have any idea why or how to solve this?
Thanks again
When Upgrading rails application from 3.2.17 to 4.0.4, I am getting this errors
default_controller_and_action': 'Sessions' is not a supported controller name. This can lead to potential routing problems. See http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use (ArgumentError)
In my routes file content
devise_for :users, :controllers => { :sessions => "Sessions", :passwords => "Passwords", :registrations => "registrations" } , :path => '', :path_names => {
:sign_in => 'login',
:sign_out => 'logout'
}
root :to => "children#index"
And SessionsController is extended from devise controller as follow
class SessionsController < Devise::SessionsController
...........................
....................
end
Why I am getting Sessions is not supported controller name ? I have try to change it in routes and controller but still facing same problem.
Solve this issues. This issues was due to case sensitive. We have to use small letter instead of Capital letter. For example, not Sessions but sessions. I was using Sessions in routes file but I have change it as sessions. The above code can written as
devise_for :users, :controllers => { :sessions => "sessions", :passwords => "passwords", :registrations => "registrations" } , :path => '', :path_names => {
:sign_in => 'login',
:sign_out => 'logout'
}