Validate attachment format for specific models in Rails 4 - ruby-on-rails

I have a polymorphic association with an Attachment model with profiles and documents tables. I have included following code in my attachment.rb:
class Attachment < ActiveRecord::Base
belongs_to :attachable, polymorphic: true
has_attached_file :attach,
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:url => ":s3_domain_url",
:path => "/contents/:id/:basename.:extension"
validates_attachment_content_type :attach,
:content_type => ['application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/pdf',
'image/jpeg', 'image/jpg', 'image/png']
end
and in my profile.rb
class Profile < ActiveRecord::Base
has_many :attachments, as: :attachable
accepts_nested_attributes_for :attachments
end
in my document.rb
class Document < ActiveRecord::Base
has_many :attachments, as: :attachable
accepts_nested_attributes_for :attachments
end
My requirement is when I save my profile it will validate only the images format, and when I save documents it will validate only the application format. Please guide me on how to solve this.

attachment.rb
validates_attachment :attach, :presence => true, :with => %r{\.(jpeg|jpg|png)$}i, :if => Proc.new{|f| f.attachable_type == 'Profile' }

Related

interpolating polymorphic relation type in url paperclip

I have a photo model which is polymorphic
class Photo < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
has_attached_file :image,
:styles => {:thumb => '120x120>', :medium => '640x480>' },
:default_style => :thumb,
:url => "/uploads/photos/#{model.imageable_type.to_s.pluralize.underscore}/:id/:basename.:extension"
and
class Question < ActiveRecord::Base
has_one :photo, as: :imageable, dependent: :destroy
class Answer < ActiveRecord::Base
has_one :photo, as: :imageable, dependent: :destroy
this generates the url as uploads/photos/Photo/some_id/file_name.ext
but I want
public/uploads/photos/questions/1/image.jpg
and
public/uploads/photos/answers/1/image.jpg.
Solved it myself.
:url => "/uploads/photos/:imageable_name/:id/:basename.:extension"
Paperclip.interpolates :imageable_name do |attachment, style|
attachment.instance.imageable_type.pluralize.downcase
end

How to validate attachment in nested model?

I have a Design model that has two Paperclip attachments inside two associations (full_image and preview). I want to be able to save designs only when both full_image and preview have valid files but can't seem to be able to make it work. Right now this is what I expected to work, but it doesn't just doesn't validate the attachments when I submit the form.
class Design < ActiveRecord::Base
has_one :full_image, :as => :assetable, :class_name => "FullImage", :dependent => :destroy
has_one :preview , :as => :assetable, :class_name => "Preview" , :dependent => :destroy
accepts_nested_attributes_for :full_image, :preview
validates_associated :preview, :full_image
end
class Asset < ActiveRecord::Base
belongs_to :assetable, :polymorphic => true
delegate :url, :to => :attachment
end
class FullImage < Asset
has_attached_file :attachment
validates_attachment_presence :attachment
end
class Preview < Asset
has_attached_file :attachment
validates_attachment_presence :attachment
end
Could someone please suggest what I should be doing?
Try :
validates :attachment, :presence => true
inside the associated model instead of validates_attachment_presence
Here is how I got it working
class Design < ActiveRecord::Base
has_one :full_image, :as => :assetable, :class_name => "FullImage", :dependent => :destroy
has_one :preview , :as => :assetable, :class_name => "Preview" , :dependent => :destroy
accepts_nested_attributes_for :full_image, :preview
validates_presence_of :preview
validates_presence_of :full_image
end
class Asset < ActiveRecord::Base
belongs_to :assetable, :polymorphic => true
delegate :url, :to => :attachment
end
class FullImage < Asset
has_attached_file :attachment
end
class Preview < Asset
has_attached_file :attachment
end

Paperclip: Product with multiple Attachments

I have a Product model that looks like this:
class Product < ActiveRecord::Base
attr_accessible :name_en, :ean
has_many :images, :dependent => :destroy
end
and an Images model that looks like this
class Images < ActiveRecord::Base
attr_accessible :image, :product_id, :source
has_attached_file :image, :styles => { :medium => "150x150>" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:bucket => "test",
:path => "/products/:ean/:style.:extension";
belongs_to :product
end
What is the easiest way to save images from an url to s3 and can I use the :ean value from the Product model for the s3 path?
use the active record callbacks.....
before_save :push_to_s3
or
after_save :push_to_s3
and then you just define your push to s3 function

Nested forms dont work in rails_admin

Brand has many images is my association. Image uses paperclip
In Rails Admin, I want to add images when i add a brand
class Brand < ActiveRecord:Base
has_many :images, as=> :imageable
end
class Image < ActiveRecord:Base
attr_accessor :image_thumb
attr_accessible :image, :imageable_id, :imageable_type, :image_thumb
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }
belongs_to :imageable, :polymorphic => true
end
But this is what i get
How can i achieve my goal?
I need to add attr_accessible
class Brand < ActiveRecord::Base
attr_accessible :name, :images_attributes
has_many :products
has_many :images, :as => :imageable
accepts_nested_attributes_for :images, :allow_destroy => true
end
And solution is when you user acts_as_taggable, to add to your rails_admin.rb config file next code:
field :tag_list do
label "Tag"
end
Or 'image_list', depends on what you are use.

How to create pages by the click of a link in ruby on rails?

I'm following this tutorial: http://railscasts.com/episodes/253-carrierwave-file-uploads or atleast I want to but would like to know if there are any tutorials around that explain how to give my users the ability to create pages(galleries) on the fly?
I intend to treat pages as albums.
They click create album link, fill in an album title.
A new page is created and from this page the user can upload photos onto the page.
kind regards
Albums and photos are just simple models. You can create controllers for them. Here is little example:
class Album < ActiveRecord::Base
belongs_to :user
has_many :album_works
validates :title, :description, :user_id, :presence => true
attr_accessible :title, :description
end
And for album work:
class AlbumWork < ActiveRecord::Base
belongs_to :album
has_many :album_work_comments
has_attached_file :photo,
:styles => {
:preview=> "860x",
:slider => "618x246#",
:thumb => "315x197#",
:homework_main => "532x355#",
:homework_mini => "184x122#",
:big_preview => "800x600#"
},
:path => ":rails_root/public/system/album_works/:style_:id.:extension",
:url => "/system/album_works/:style_:id.:extension",
:default_url => "/images/photo_holder.png"
validates_attachment_size :photo, :less_than => 2.megabytes
validates_attachment_content_type :photo, :content_type => ['image/png', 'image/jpeg', 'image/jpg', 'image/bmp']
attr_accessible :title, :photo
validates :title, :album_id, :presence => true
end
Now you should create corresponding controllers and views. But they are just simple rails controllers and views. Note that I'm using paperclip, but it's only an example to show how it can be done.

Resources