Skip to content

Instantly share code, notes, and snippets.

@bamnet
Created June 29, 2010 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bamnet/457747 to your computer and use it in GitHub Desktop.
Save bamnet/457747 to your computer and use it in GitHub Desktop.
<% @content.medias.build({:key=>"original"}) %>
<%= form_for(@content, :html => { :multipart => true }) do |f| %>
<div class="field">
<%= f.fields_for :medias do |media| %>
<%= media.file_field :file %>
<%= media.hidden_field :key %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= form_for(@content) do |f| %>
<div class="field">
<%= f.label :data %><br />
<%= f.text_area :data %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
class Content < ActiveRecord::Base
belongs_to :user
belongs_to :kind
has_many :submissions, :dependent => :destroy
has_many :feeds, :through => :submissions
has_many :medias, :as => :attachable
accepts_nested_attributes_for :medias
accepts_nested_attributes_for :submissions
#Validations
validates :name, :presence => true
validates :kind, :presence => true, :associated => true
validates :user, :presence => true, :associated => true
validates :duration, :numericality => { :greater_than => 0 }
#Easily query for active, expired, or future content
scope :expired, where("end_time < :now", {:now => Time.now})
scope :future, where("start_time > :now", {:now => Time.now})
scope :active, where("(start_time IS NULL OR start_time < :now) AND (end_time IS NULL OR end_time > :now)", {:now => Time.now})
#Scoped relations for feed approval states
has_many :approved_feeds, :through => :submissions, :source => :feed, :conditions => {"submissions.moderation_flag" => true}
has_many :pending_feeds, :through => :submissions, :source => :feed, :conditions => "submissions.moderation_flag IS NULL"
has_many :denied_feeds, :through => :submissions, :source => :feed, :conditions => {"submissions.moderation_flag" => false}
# Determine if content is active based on its start and end times.
# Content is active if two conditions are met:
# 1. Start date is before now, or nil.
# 2. End date is after now, or nil.
def is_active?
(start_time.nil? || start_time < Time.now) && (end_time.nil? || end_time > Time.now)
end
end
class Graphic < Content
#Validations
validates :duration, :numericality => { :greater_than => 0 }
end
<% content_for :left_sidebar do %>
<ul>
<% Content.subclasses.each do |subclass| %>
<li><%= link_to subclass.model_name.human, new_content_path(:type=>subclass.model_name.singular) %></li>
<% end %>
</ul>
<% end %>
<%= render 'contents/forms/#{type_will_go_here}' %>
class Ticker < Content
#Validations
validates :duration, :numericality => { :greater_than => 0 }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment