Paperclip separate styles - ruby-on-rails

How can i separate styles for different models?
I have my Photo model which generate my styles
large:'300x300#',huge:'800x800'
also i have two models witch use this styles
Product
and
Post
so i want to use
large style only for Product
and huge style only for Post
product => has_many :photos
post => has_one :photo
photo => belongs_to :post
belongs_to :product
has_attached_file :image, :styles => {large:'300x300#',huge:'800x800'}
Is it possible?

I advice you to separte image by a image model type using STI, and polymorphism, therefore, add imageable_type, imageable_id, and type field to photo model, so:
app/models/photo.rb:
class Photo < AR::Base
belongs_to :imageable, polymorphic: true
end
app/models/photos/large_photo.rb:
class LargePhoto < Photo
has_attached_file :image, :styles => { large:'300x300#' }
end
app/models/photos/huge_photo.rb:
class HugePhoto < Photo
has_attached_file :image, :styles => { huge:'800x800' }
end
app/models/product.rb:
class Product < AR::Base
has_many :large_photos, as: imageable
end
app/models/post.rb:
class Post < AR::Base
has_one :huge_photo, as: imageable
end
BUT for me, will be better to use carrierwave, rather than paperclip for this case.

Related

Rails 5 - polymorphic attachment with dynamic styles using paperclip

I am trying to have a polymorphic attachment class with dynamic styles using paperclip and use it form another class(Foo).
I have the Attachment class:
class Attachment < ApplicationRecord
belongs_to :attachable, polymorphic: true, optional: true
has_attached_file :attachment,
styles: ->(a) {a.instance.attachable.attachment_styles}
validates_attachment_content_type :attachment, content_type: /\Aimage\/.*\Z/
end
And the Foo class:
class Foo < ApplicationRecord
has_many :attachments, class_name: "Attachment", as: :attachable, dependent: :destroy
accepts_nested_attributes_for :attachments
def attachment_styles
{
:thumb => "30x30>",
:medium => "100x100>"
}
end
end
The problem here is that I don't have the foo object yet, inside that lambda function. a.instance.attachable returns nil.
The error I'm getting is:
NoMethodError: undefined method `attachment_styles' for nil:NilClass
I am using Rails 5 and paperclip 5.0.0.
I think this worked with Rails 4.
PS: I need the style to be dynamic because I will use the attachment from multiple models, not just from Foo.

Rails model with associated images and selecting main image for this model

I want to ask about, how can you properly select main image from association of images after you update main_image and delete more couple images etc.
I have this code:
It's the main model:
class Gallery < ApplicationRecord
has_many :paintings, inverse_of: :gallery
belongs_to :primary_painting, class_name: 'Painting', required: false
accepts_nested_attributes_for :paintings, reject_if: :all_blank, allow_destroy: true
before_validation :primary_painting_setup
private
def primary_painting_setup
self.primary_painting = paintings.any? ? paintings.select(&:primary?).first || paintings.first : nil
end
end
And this is associated model:
class Painting < ApplicationRecord
belongs_to :gallery, inverse_of: :paintings
has_attached_file :image, styles: { medium: '300x300>', thumb: '100x100>' }, default_url: '/images/:style/missing.png'
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end
Basically, gallery has main_image on index page, which is chosen from associate paintings.
But f.e. if I delete all images and add a new one and set it as main image. It won't work, because on the stage of before_validation I have only old images, which aren't useful for me at all.
Of course I can use a helper or model method such as gallery.paintings.find(:&main_image_bool) every time I render a gallery, but it costs too much speed.
Can you advise more proper way to choose this? Maybe there's a magic association, that will help.

Can't add existing has many models to non-existing parent model in Rails app

I have 2 models - Widget and Photo. Widget has_many photos and set accepts_nested_attributes_for :photos.
I upload photos before creating of widget, so params comes with null widget_id attribute in photo model. And when I'm trying to save widget it says that can't find photo with proper widget_id.
How to handle it? Are there any hooks for it? I can't create Widget before photos
class Widget < ActiveRecord::Base
belongs_to :user
has_many :photos, dependent: :destroy
accepts_nested_attributes_for :photos
default_scope {order('position ASC')}
end
class Photo < ActiveRecord::Base
belongs_to :widget
has_attached_file :image
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end
Stacktracke: http://pastebin.com/qMDmwdbx

Rails model multiple properties of the same model

I'm struggle to find a way to have more than one property of the same model. I've this structure :
class Image < ActiveRecord::Base
attr_accessible :name, :content_type
end
class MenuImage < ActiveRecord::Base
belongs_to :image
belongs_to :menu
end
class Menu < ActiveRecord::Base
has_many :menu_images
has_many :images, :through => :menu_images
has_one :image, :as => :thumbnail_image
attr_accessible :thumbnail_image_id
end
I want to be able to access the images using #menu.images and #menu.thumbnail_image.
The code has_one :image, :as => :thumbnail_image is an example of what i'm trying to obtain.
You name it differently, but tell ActiveRecord to use the Image class:
has_one :thumbnail_image, :class_name => "Image"
See also the Association Guide: http://guides.rubyonrails.org/association_basics.html#has_one-association-reference

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.

Resources