Dynamic Store Dir with CarrierWave - ruby-on-rails

Only new to Rails but I've started been using CarrierWave for my upload handling. I am trying to make a media hosting app whereby:
A Show belongs to a User
A Show has many Episodes
An episode belongs to video
At the moment I am using single table inheritance to store my video content into a meta data type Asset table, all is saving well. The issue I'm trying to get to is when I save the video via CarrierWave I want to be able to get both the show slug and the episode slug into the save path.
Currently I have this set up with my shows and episode uploads:
def store_dir
"shows/#{model.friendly_id}/cover/"
end
And previously before STI i would just store the filename in a video field in the episodes table and have it store via:
def store_dir
"shows/#{model.show.slug}/episodes/#{model.id} - #{model.friendly_id}"
end
Only problem now I cannot get the episode information from my video model in the CW uploader.
My models are:
Show.rb
class Show < ActiveRecord::Base
#Associations
belongs_to :user
belongs_to :category
has_many :episodes
#Slugs
extend FriendlyId
friendly_id :title, use: :slugged
#Carrierwave
mount_uploader :image, ShowImageUploader
end
Episode.rb
class Episode < ActiveRecord::Base
belongs_to :show
belongs_to :image, :class_name => "Episode::Image"
belongs_to :audio, :class_name => "Episode::Audio"
belongs_to :video, :class_name => "Episode::Video"
accepts_nested_attributes_for :video, :image, :audio
before_save :add_guid, :on => :create
#Slugs
extend FriendlyId
friendly_id :title, use: :slugged
validates :title, :presence => true, :length => {:within => 5..40}
protected
def add_guid
self.guid = UUIDTools::UUID.random_create.to_s
end
end
episode/video.rb
class Episode::Video < Asset
mount_uploader :video, EpisodeVideoUploader, :mount_on => :filename
end
asset.rb
class Asset < ActiveRecord::Base
end
So I guess how can I get the appropriate data after upload into:
"shows/#{model.show.slug}/episodes/#{model.id} - #{model.friendly_id}"
Thanks!

Related

Paperclip separate styles

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.

How to customize the categories and sub categories in rails

Help!
It is impossible to realize the right category and subcategory, please help me.
My code
Category.rb
class Category < ActiveRecord::Base
extend FriendlyId
friendly_id :title, use: :slugged
has_many :scategories, :dependent => :destroy
validates :title, presence: true
end
scategory.rb
class Scategory < ActiveRecord::Base
extend FriendlyId
friendly_id :title, use: :slugged
belongs_to :category
has_many :posts, :dependent => :destroy
end
post.rb
class Post < ActiveRecord::Base
belongs_to :scategory
belongs_to :user
end
_post.html.haml
.column4.post-block.item
/ Отложенная загрузка картинок "lazy: true" (исправить баг)
= link_to image_tag(post.post_cover.url(:medium)), post
.title
= link_to truncate(post.title, :length => 70), category_scategory_post_url(category_id: #category.slug, scategory_id: #scategory.slug, id: #post.slug)
-#
.description
= truncate(post.description, :length => 70)
routes.rb
resources :categories do
resources :scategories do
resources :posts
end
end
Can not find a method of slug, how to properly do?
change it to #post = Post.friendly.find_by_slug(params[:slug]).
also change your category and scategory controller like above.
p.s. before these check you've permitted :slug attribute.
p.p.s. also consider this answer for editing routes

validate count of created has_many objects in rails 3.2

Validate count of created has_many objects in rails 3.2?
I need a custom validation for "max/min" count of associated object.
I have Realty, that has
has_many :realty_images, :dependent => :destroy
accepts_nested_attributes_for :realty_images
and realty_image:
class RealtyImage < ActiveRecord::Base
attr_accessible :avatar, :image, :realty_id
belongs_to :realty
#here a suppose I need to put some kind of custom validation
mount_uploader :image, ImageUploader
end
The standard validation methods work well with associations:
class Ad
has_many :realty_images
# make sure there are some images
validates_presence_of :realty_images
# or make sure the number of images is in certain range
validates_length_of :realty_images, within: 5..10
end
Check out documentation for more details.
Not sure if I totally understood, but if you try to limit the number of realty_images of a given realty, and assuming that realty.maximum contains the max limit for that given realty:
In RealtyImage model:
class RealtyImage < ActiveRecord::Base
attr_accessible :avatar, :image, :realty_id
belongs_to :realty
validate :maximum_number_of_realty_images
mount_uploader :image, ImageUploader
protected
def maximum_number_of_realty_images
errors.add(:base, "Maximum reached") unless realty.realty_images.count < realty.maximum
end
end

Rails Carrierwave stor_dir location not valid (Model.#{association} returns nil)

I am using carrierwave to upload images to my webapp.
It has become necessary to upload them to the location of the parent model.
Ie.
The parent is house which has many images.
So I want to store the images in
public/uploads/houses/images/[:house_id]/
This is my current setup.
..uploaders/image_uploader.rb
def store_dir
puts "uploads/house/#{model.house_id}/#{mounted_as}/#{model.id}"
"uploads/house/#{model.house_id}/#{mounted_as}/#{model.id}"
end
The puts statement prints out the correct path that I would like but the path saved does not match.
It appears that the model.house_id is returning nil
House Model
class House < ActiveRecord::Base
attr_accessible :address, :description, :title, :price, :image, :image_id, :images, :image_cache
has_many :images
mount_uploader :image, ImageUploader
end
Image Model
class Image < ActiveRecord::Base
attr_accessible :house_id, :image
mount_uploader :image, ImageUploader
belongs_to :house
end
How do I get the correct path/ What am I doing wrong :(
Please try:
def cache_dir
"#{Rails.root}/public/uploads/houses/images"
end
def store_dir
"#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

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

Resources