carrierwave same callback for multiple uploaders - ruby-on-rails

I've got a rails application with multiples uploaders.
In each uploaders I'd like a function to be executed after each file delete.
I can put a trigger and a callback in each uploader :
after :remove, :remove_dir
def remove_dir
FileUtils.remove_dir("#{Rails.root}/public/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}", :force => true)
end
But this is not really smart...
There is probably a better way to do that ????

The easiest solution would be to create a parent class for all uploader and then inherit other uploaders from it.
If you place a callback in the parent class it will be executed by all the children. Parent class should of course inherit from CarrierWave::Uploader::Base
Another solution would be to create a module and include it in all uploader classes.

I think that I've finally found a convenient solution thanks to Michal Szyndel that helped me and gave me good clues :
Each uploaders inherit from CarrierWave::Uploader::Base, so I add a callback to it through the carrierwave's initializer file(carrierwave.rb), and then I call it thanks to a trigger located in the concerned uploaders.
in carrierwave.rb :
module CarrierWave
module Uploader
class Base
def remove_dir
path = "#{Rails.root}/public/"+store_dir
FileUtils.remove_dir(path, :force => true) if Dir[path+'/*'].empty?
end
end
end
end
in concerned uploaders :
after :remove, :remove_dir

Related

Why do functions from my Rails plugin not work without specifically requiring?

I need some help with my plugin. I want to extend ActiveRecord::Base with a method that initializes another method that can be called in the controller.
It will look like this:
class Article < ActiveRecord::Base
robot_catch :title, :text
...
end
My attempt at extending the ActiveRecord::Base class with robot_catch method looks like following. The function will initialize the specified attributes (in this case :title and :text) in a variable and use class_eval to make the robot? function available for the user to call it in the controller:
module Plugin
module Base
extend ActiveSupport::Concern
module ClassMethods
def robot_catch(*attr)
##robot_params = attr
self.class_eval do
def robot?(params_hash)
# Input is the params hash, and this function
# will check if the some hashed attributes in this hash
# correspond to the attribute values as expected,
# and return true or false.
end
end
end
end
end
end
ActiveRecord::Base.send :include, Plugin::Base
So, in the controller, this could be done:
class ArticlesController < ApplicationController
...
def create
#article = Article.new(params[:article])
if #article.robot? params
# Do not save this in database, but render
# the page as if it would have succeeded
...
end
end
end
My question is whether if I am right that robot_catch is class method. This function is to be called inside a model, as shown above. I wonder if I am extending the ActiveRecord::Base the right way. The robot? function is an instance method without any doubt.
I am using Rails 3.2.22 and I installed this plugin as a gem in another project where I want to use this functionality.
Right now, it only works if I specifically require the gem in the model. However, I want it the functionality to be included as a part of ActiveRecord::Base without requiring it, otherwise I'd have to require it in every model I want to use it, not particularly DRY. Shouldn't the gem be automatically loaded into the project on Rails start-up?
EDIT: Maybe callbacks (http://api.rubyonrails.org/classes/ActiveSupport/Callbacks/ClassMethods.html) would be a solution to this problem, but I do not know how to use it. It seems a bit obscure.
First, I would suggest you make sure that none of the many many built in Rails validators meet your needs.
Then if that's the case, what you actually want is a custom validator.
Building a custom validator is not as simple as it might seem, the basic class you'll build will have this structure:
class SpecialValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
# Fill this with your validation logic
# Add to record.errors if validation fails
end
end
Then in your model:
class Article < ActiveRecord::Base
validates :title, :text, special: true
end
I would strongly suggest making sure what you want is not already built, chances are it is. Then use resources like this or ruby guides resources to continue going down the custom validator route.
Answer
I found out the solution myself. Bundler will not autoload dependencies from a gemspec that my project uses, so I had to require all third party gems in an engine.rb file in the lib/ directory of my app in order to load the gems. Now everything is working as it should.
Second: the robot_catch method is a class method.

Rails4 Application Model

i have the following function that i make use of in a lot of my models. i use MongoID for MongoDB wrapper
def make_slug
self.slug = self.name.downcase.gsub(/[^a-z1-9]+/, '').chomp('')
end
Which is the best place to place it than copy and paste it in all my models.
Also any recommendation for a good Slug Gem for Rails4?
All models are Inherited from ActiveRecord, you can open the eigenclass to add a singleton method there and use in all models.
The method I'll choose would be putting it under lib directory and require it in each model I need it.
I guess you could do a mixin/module, which you include in your models where you need the functionality. Like this:
Example of the module:
module SlugMaker
def make_slug
# Do your magic here
end
end
And then include it in your model:
class SuperAwesomeModel
include SlugMaker
def some_action
make_slug
end
end

Carrierwave store_dir not modified when running rspec with spork

I've followed the instructions on how to define a test-specific store directory for carrierwave uploads, which suggests opening the CarrierWave::Uploader::Base class and redefining store_dir and cache_dir like so:
if defined?(CarrierWave)
CarrierWave::Uploader::Base.descendants.each do |klass|
next if klass.anonymous?
klass.class_eval do
def cache_dir
"#{Rails.root}/spec/support/uploads/tmp"
end
def store_dir
"#{Rails.root}/spec/support/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
end
end
This works like a charm, except when I run rspec with spork, then it fails to modify the class and uploads are stored in the default location. Does anyone have any clue why this might be happening?
Someone else working on our project solved this problem by adding a line with just AvatarUploader ahead of the CarrierWave::Uploader::Base.descendants.each line, like this:
if defined?(CarrierWave)
AvatarUploader # load AvatarUploader class
CarrierWave::Uploader::Base.descendants.each do |klass|
#...
Not entirely sure why this works, but it does.
Little addition to accepted answer, for anyone coming here:
if uploader classes are not loaded before the call to
CarrierWave::Uploader::Base.descendants, it will return empty array, so either specify each uploader like in accepted answer or you could do something like this to require all uploaders from let's say rails uploaders folder
Dir["#{Rails.root}/app/uploaders/*.rb"].each {|file| require file}
CarrierWave::Uploader::Base.descendants.each do |klass|
#...

Displaying a Carrierwave filename in the view

I am trying to display the filename of a Carrierwave attachment in a Rails erb template. The following does not work:
<%= #page.form.filename %>
This seems in line with the documentation. Is some additional step needed?
My page model looks like this:
class Page < ActiveRecord::Base
mount_uploader :form, FormUploader
end
The form uploader looks like this:
class FormUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def extension_white_list
%w(pdf)
end
end
I have been able to get the filename via the file internal parameter:
<%= #page.form.file.filename %>
The documentation you're looking at is the sanitized file, it's what it uses for actually storing a file. The part you're looking for is FormUploader, which is an Uploader, and part of http://rubydoc.info/gems/carrierwave/0.5.2/CarrierWave/Uploader
If you want to get the file name, you could either read it from the database column directly, or use File.basename(#page.form.path) to extract it easily.
The Carrierwave docs might be a bit off, but recommended way seems to be:
#page.form.file.identifier
#adamonduty's solution is great. Another solution I used before, just create a method on the model:
def name
file.path.split("/").last
end
You're right #epylinkn. Documentation points towards using:
#page.form.file.identifier
But when I use that, I always get nil (just as #Cheng commented).
I then inspected my objects methods (#page.form.file.methods.inspect), and found the following to work:
#page.form.file_identifier
In your model's associated uploader class, define a filename method.
def filename
File.basename(path)
end
You can then call
model_instance.file.filename
Works as of CarrierWave 1.1.0. This is a succinct restatement/amalgamation of kikito and Chris Alley's responses above.
If you're using ActiveRecord, you can directly access the field named form in two ways:
def my_method
self[:form]
end
or
def my_method
form_before_type_cast
end
The second method is read-only.
CarrierWave::SanitizedFile has a private original_filename method containing the filename of the uploaded file. (docs: http://rdoc.info/github/jnicklas/carrierwave/master/CarrierWave/SanitizedFile:original_filename)
After reading through this thread from the CarrierWave mailing list, none seemed to fit my needs. With something like
class Upload < ActiveRecord::Base
mount_uploader :file, FileUploader
# ...
I heavily modify the :file column value from the original filename. Due to this I decided to track the original filename in a separate column from the one bound to CarrierWave. In my FileUploader I simply added a reader that wraps the private original_filename method:
def original_file
original_filename
end
I then added a before_create event to the Upload class (my Upload records are never modified, so a before_create is acceptable for my needs)
before_create do
self.original_file = self.file.original_file
end
I'm assuming you've got models like this?
class Page
mount_uploader :form, FormUploader
end
If so you should be able to call:
#page.form.url
#page.form.filename
Are you sure you've uploaded/attached the file correctly? What do you see when you inspect #page.form? Remember, the attachment will not be saved until you've fully processed the upload.
This is my solution:
before_save :update_file_attributes
def update_file_attributes
if file.present? && file_changed?
self.content_type = file.file.content_type
self.file_size = file.file.size
self.file_name = read_attribute(:file)
end
end

How delete model with linked file

I see by default carrierwave does not delete files linked to model.
How to do it?
Carrierwave should remove the files from S3 automatically for you. I just tested this out on a Rails 3.1 app.
You need to call
#image.destroy
not
#image.delete
Also use refresh button on aws s3 panel
I'm not familiar with carrierwave, but in general, hooking into the after_destroy is likely what you want.
class Model < ActiveRecord::Base
after_destroy :delete_linked_file
def delete_linked_file
# Delete the linked file here
end
end
Yes,
You can do it like this
def delete_image_folder
FileUtils.remove_dir(File.join(Rails.root, File.join( 'public' , file_name.store_dir)), :force => true)
end
but just remember that if you changed the Carrierwave configuration root, you should take it into account (default is public so this code will work)

Resources