Skip to content

Instantly share code, notes, and snippets.

@Olefine
Created August 13, 2013 10:42
Show Gist options
  • Select an option

  • Save Olefine/6219990 to your computer and use it in GitHub Desktop.

Select an option

Save Olefine/6219990 to your computer and use it in GitHub Desktop.
rails model post
class Post < ActiveRecord::Base
resourcify
belongs_to :user
belongs_to :journal, :polymorphic => true
has_many :images, :class_name => "PostImage", :as => :imageable, :dependent => :destroy
has_one :illustration, :class_name => "PostIllustration", :as => :imageable, :dependent => :destroy
accepts_nested_attributes_for :illustration
accepts_nested_attributes_for :images,\
:reject_if => proc {|attributes| attributes['image'].blank?\
&& attributes['image_cache'].blank?}, :allow_destroy => true
acts_as_taggable
acts_as_votable
has_many :videos, :class_name => "MediaContent", :as => :multimedia, :conditions => ["media_contents.video = ?", true]
acts_as_commentable
DEPTH = 1
has_many :likes, :as => :likeable
has_many :complaints, :as => :pretension, :dependent => :destroy
has_many :favorites, :as => :favoriteable
attr_accessible :body, :title, :illustration_attributes, :images_attributes, :tag_list, :status
attr_accessible :body, :title, :illustration_attributes, :images_attributes, :tag_list, :status, :as => :admin
validates :user, :journal, :presence => true
validates :title, :body, :illustration, presence: true, on: :update
default_scope order("#{table_name}.created_at DESC")
scope :not_recommended, where(:status => 0)
scope :recommended, where(:status => 1)
scope :accepted, where(:status => 2)
slice :body, :as => :preview, :slice => {:maximum => 250}, :resize => {:width => 300}
def owner?(user)
self.user_id == user.id
end
def root_object
journal.try(:root_object)
end
def tags_string
tags.map(&:text)*","
end
def accepted?
status == 2
end
def recommended?
status == 1
end
def accept!
self.status = 2
save
end
def unaccept!
self.status = 0
save
end
def update_attributes_with_video (params, urls)
urls||=[]
if update_attributes(params)
urls.reject(&:empty?).each do |url|
video = self.videos.create(content: url, title: '"'+ params[:title]+'"'+"- video",video: true)
return false unless video.errors.empty?
end
return true
else
return false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment