Paperclip multiple file content types - ruby

I want to be able to upload and validate a particular file type based on different views on the same model which is UploadedFile
This is what I have so far below and I want to be able to use imageable as polymorphic association with other models and validate depending on what controller is processing specific action. For example, I have form that submits images for a particular view then another one for submitting videos.
class UploadedFile < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
has_attached_file :assets
validates_attachment :assets,
:content_type => /^image\/(png|gif|jpeg)/,
:default_url => "/",
:message => "only (png|gif|jpeg) images are allowed and the size cannot exceed 5mb"
:size => { :in => 0..5000.kilobytes }
end
So what I need is if image is submitted, I validate according to image validation_attachment settings then change this to video settings if submitted from the video form.
How would I go about this with Paperclip and Rails 4?

Related

Set the field as required in ActiveAdmin [duplicate]

What is the simplest way to make a field required in Rails?
inquiry.rb:
class Inquiry < ActiveRecord::Base
attr_accessible :address, :email_id, :gender, :message, :mobile_number, :name
end
You can use the presence validator:
validates :name, :presence => true
attr_accessible specifies a white list of model attributes that can be set via mass-assignment. This is meant to protect sensitive attributes from being overwritten by malicious users tampering with URLs or forms. It has nothing to do with validations.
So, if you want to make the attribute presence mandatory, you have to use a validation in your model, like this one:
validates :name, :presence => true

ruby on rails "before_save" in parent model to check attribut of child

I have a model 'place_detail' that has many a child 'emails'
has_many :emails, :dependent => :destroy
and in the email model:
belongs_to :place_detail
Now in the place_detail i want to make sur i added a email to check a attribut 'has_email'
so i added:
before_save :check_if_has_email
...
def check_if_has_email
if emails.count >0
self.has_email = true;
else
self.has_email = false;
end
end
the problem is that the attribute has_email does not check event if i created a email child. what i understand is that the parent is saved before the child
how can i get my has_email checked in place_detail when i create a child email?
EDIT:
I could simply put the has_email boolean in a method like
def has_email?
if self.emails.count >0
return true
..
but i prefer the boolean in the attribute because i use it in many scope and it would be a lot a change in the app
This will ensure that your model has at least one email (place it in your model file place_detail.rb)
has_many :emails, :dependent => :destroy
validates :emails, :length => { :minimum => 1 }
EDIT:
One suggestion would be just to check the trait place_detail.emails count when you need it. If you examine such data multiple times a request you can store it like so
def has_email?
(result ||= self.emails.count) > 0
end
That way it will only check your database once
If you're forced to use the 'has_email' attribute within the place_detail model, you could simply save the place_detail in the Create method of the Email controller.
#email = Email.new(email_params)
#email.place_detail.save

Using paperclip gem in rails3

Using paperclip gem in rails3, there are two copies of image uploaded simultaneously of which one is having null entries and the other is original in the database as I checked in localhost/phpmyadmin. This problem unnecessarily populates my database. Have been searching for quite a few days. Reviewed many answers regarding multiple images but no one mentioned about this problem.
I've followed this code https://github.com/websymphony/Rails3-Paperclip-Uploadify.
Paperclip was also uploading the actual image data into the field image in my database. I had to tweak it to save file names in the image_file_name field in my database.
Here is my controller that saves the image from the upload form.
#paperclip replaces spaces with _
formatted_filename = params[:clothe][:image].original_filename
formatted_filename.gsub!(/\s/,'_')
#hook in image processing
#set type of upImg, formUpload (APIUpload, scrapeUpload, mobileUpload)
image = UploadImage.new(formatted_filename, Rails.root.to_s + '/public/products/', #clothe.id)
image.processImage
Here is my model
class Product < ActiveRecord::Base
attr_accessible :description, :price, :title, :image, :image_file_name, :published
has_attached_file :image,
:styles => {
:thumb => "100x100#",
:small => "150x150>",
:medium => "200x200" },
:default_url => '/assets/missin.gif',
:path => Rails.root.to_s + "/public/products/:filename",
:url => "/products/published/:basename.:extension"

How to retrieve an object from a CouchDB view using the CouchRest Ruby Gem

Assuming I have a view in my CouchDB named "user/all" and a CouchRest ExtendedDocument as follows:
class User < CouchRest::ExtendedDocument
property :username
property :password
property :realname
property :role
property :rights
end
How would I go about retrieving a document for the key 'admin' from this view using this ExtendedDocument?
(If I need to make changes to the ExtendedDocument subclass, what should be changed?)
Many thanks.
Try this:
class User < CouchRest::ExtendedDocument
property :username
property :password
property :realname
property :role
property :rights
view_by :role
end
Here, I am assuming 'admin' is a role property. This will make a view in your design document keyed by role. Then, to get all 'admin' documents, you just do the following:
#admins = User.by_role(:key => 'admin')
If in fact the actual id of the document is 'admin', then all you have to do is this:
#admin = User.get('admin')
Or, alternatively:
#admin = User.all(:key => 'admin')
I would also suggest taking a look at CouchRest Model, which is basically an Active Model complaint extension to CouchRest if you are using this with Rails. Good Luck!

Editing records with SQLite, DataMapper, and Sinatra

I'm in the process of learning Sinatra and DataMapper. To do so, I've been playing with a "customer database" project.
Creating and deleting records is trivial and now I'm working on editing records. So far I've managed to piece together a form in my views and a couple of routes that I thought would edit a record. Here's some code to illustrate my issue:
My edit.erb view: http://gist.github.com/308405
My edit/update routes:
get '/edit/:acct' do
#title = "Edit Client Data"
#client = HE_Backend.get(params[:acct])
erb :edit
end
post '/update/:acct' do
client = HE_Backend.get(params[:acct])
client.attributes = {
:name => params['client']['name'],
:company => params['client']['company'],
:street => params['client']['street'],
:state => params['client']['state'],
:zip => params['client']['zip'],
:phone => params['client']['phone'],
:fax => params['client']['fax'],
:website => params['client']['website'],
:order_date => params['client']['order_date'],
:payment_date => params['client']['payment_date'],
:monthly => params['client']['monthly'],
:setup => params['client']['setup'],
:details => params['client']['details'],
:notes => params['client']['notes'],
:status => params['client']['status'],
}
if client.save
redirect "/show/#{client.acct}"
else
redirect('/list')
end
end
It looks like the "client.save" portion of the route is returning false, because I'm getting redirected to "/list" each time. If I use the #update method rather than #save, DM complains about "dirty records".
Anyone have any ideas as to what I'm doing wrong or can you point me to examples for editing records in SQLite with DataMapper and Sinatra?
Thanks!
This turned out to be a validations issue. If I don't have validations in place and put data types other than what's in my model in those fields, the #save method apparently returns false.

Resources