Skip to content

Commit

Permalink
Updating post story forms to include media item elements.
Browse files Browse the repository at this point in the history
  • Loading branch information
Russell Branca committed Feb 17, 2010
1 parent 6bf87b3 commit 24f0864
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 15 deletions.
7 changes: 6 additions & 1 deletion app/models/audio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ class Audio < ActiveRecord::Base
belongs_to :user
belongs_to :audioable, :polymorphic => true

validates_presence_of :url
validates_format_of :url, :with => /\Ahttp(s?):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?\.mp3/i, :message => "should look like a URL ending in .mp3"

after_validation :set_user

def set_user
self.user = current_user unless self.user.present?
end

end
1 change: 1 addition & 0 deletions app/models/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Content < ActiveRecord::Base
acts_as_taggable_on :tags, :sections
acts_as_featured_item
acts_as_moderatable
acts_as_media_item

belongs_to :user
belongs_to :article
Expand Down
13 changes: 9 additions & 4 deletions app/models/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@ class Image < ActiveRecord::Base
}

before_validation :download_image, :if => :remote_image_url?
validates_presence_of :remote_image_url, :allow_blank => true, :message => 'invalid image or url.'
validates_presence_of :remote_image_url, :allow_blank => true, :message => 'invalid image or url.', :if => :remote_image_url?
validates_presence_of :image, :image_file_name, :image_content_type, :image_file_size

def url options = {}
self.image.url options
end
after_validation :set_user

delegate :url, :to => :image

private

def set_user
self.user = current_user unless self.user.present?
end

def download_image
self.image = open(URI.parse(remote_image_url))
end
Expand Down
11 changes: 6 additions & 5 deletions app/models/video.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ class Video < ActiveRecord::Base

belongs_to :user
belongs_to :videoable, :polymorphic => true
validates_presence_of :remote_video_url, :unless => :embed_code?
validates_presence_of :embed_code?, :unless => :remote_video_url?

validates_format_of :remote_video_url, :with => /\Ahttp(s?):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i, :message => "should look like a URL", :allow_blank => true
validates_format_of :remote_video_url, :with => /(youtube|vimeo).com/i, :message => "should be a youtube or video url", :allow_blank => true
validates_format_of :remote_video_url, :with => /(youtube|vimeo).com/i, :message => "should be a youtube or vimeo url", :allow_blank => true
validates_format_of :embed_code, :with => /<embed[^>]+src="http(s?):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?"/i, :message => "should look like a URL", :allow_blank => true

after_validation :process_video
after_validation :set_user

def url_video?
remote_video_url?
Expand All @@ -31,8 +30,6 @@ def video_src
end
end

#private

def process_video
if embed_code?
if embed_code =~ /<embed[^>]+?src="([^"]+)"/i
Expand Down Expand Up @@ -71,4 +68,8 @@ def parse_youtube_url url
def parse_vimeo_url url
end

def set_user
self.user = current_user unless self.user.present?
end

end
23 changes: 23 additions & 0 deletions app/views/shared/forms/_media_items.fbml.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
%h2 Upload or link to an image:
- @story.images.build if @story.images.empty?
- form.semantic_fields_for :images do |image_form|
= image_form.file_field :image
= image_form.input :remote_image_url, :required => false
%br
%br
%h2 Embed or link to a video (youtube only):
- @story.videos.build if @story.videos.empty?
- form.semantic_fields_for :videos do |video_form|
= video_form.input :embed_code, :required => false
= video_form.input :remote_video_url, :required => false
%br
%br
%h2 Link to an audio file (.mp3 only):
- @story.audios.build if @story.audios.empty?
- form.semantic_fields_for :audios do |audio_form|
= audio_form.input :url, :required => false
= audio_form.input :title, :required => false
= audio_form.input :album, :required => false
= audio_form.input :artist, :required => false
%br
%br
23 changes: 23 additions & 0 deletions app/views/shared/forms/_media_items.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
%h2 Upload or link to an image:
- @story.images.build if @story.images.empty?
- form.semantic_fields_for :images do |image_form|
= image_form.file_field :image
= image_form.input :remote_image_url, :required => false
%br
%br
%h2 Embed or link to a video (youtube only):
- @story.videos.build if @story.videos.empty?
- form.semantic_fields_for :videos do |video_form|
= video_form.input :embed_code, :required => false
= video_form.input :remote_video_url, :required => false
%br
%br
%h2 Link to an audio file (.mp3 only):
- @story.audios.build if @story.audios.empty?
- form.semantic_fields_for :audios do |audio_form|
= audio_form.input :url, :required => false
= audio_form.input :title, :required => false
= audio_form.input :album, :required => false
= audio_form.input :artist, :required => false
%br
%br
1 change: 1 addition & 0 deletions app/views/stories/_story_form.fbml.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
= form.input :image_url
= form.input :tags_string, :required => false
= form.input :caption
= render :partial => 'shared/forms/media_items', :locals => { :form => form }
1 change: 1 addition & 0 deletions app/views/stories/_story_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
= form.input :tags_string, :required => false
%ul#image_selector.jcarousel-skin-tango{:style => "left: 0px;"}
= form.input :caption
= render :partial => 'shared/forms/media_items', :locals => { :form => form }
2 changes: 1 addition & 1 deletion app/views/stories/new.fbml.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
.panel_block
.storyBlockWrap
%h3= link_to 'Post an Article', new_article_path
- semantic_form_for @story, :url => stories_path do |form|
- semantic_form_for @story, :url => stories_path, :html => { :multipart => true } do |form|
= render :partial => 'story_form', :locals => { :form => form }
- form.buttons do
-# TODO:: switch this to locale
Expand Down
2 changes: 1 addition & 1 deletion app/views/stories/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
.panel_block
.storyBlockWrap
%h3= link_to 'Post an Article', new_article_path
- semantic_form_for @story, :url => stories_path do |form|
- semantic_form_for @story, :url => stories_path, :html => { :multipart => true } do |form|
= render :partial => 'story_form', :locals => { :form => form }
- form.buttons do
-# TODO:: switch this to locale
Expand Down
2 changes: 2 additions & 0 deletions app/views/stories/show.fbml.haml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
- if @story.moderatable?
= render :partial => 'shared/flag_form', :locals => { :flaggable => @story }

- if @story.media_item?
= render_media_items @story
= render :partial => 'shared/comments', :locals => { :comments => @story.comments }
= render :partial => 'shared/comment_form', :locals => { :commentable => @story }

Expand Down
2 changes: 2 additions & 0 deletions app/views/stories/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
- if @story.moderatable?
= render :partial => 'shared/flag_form', :locals => { :flaggable => @story }

- if @story.media_item?
= render_media_items @story
= render :partial => 'shared/comments', :locals => { :comments => @story.comments }
= render :partial => 'shared/comment_form', :locals => { :commentable => @story }

Expand Down
13 changes: 10 additions & 3 deletions lib/acts_as_media_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ def self.included(base)

module ClassMethods
def acts_as_media_item
has_many :images, :as => :imageable
has_many :videos, :as => :videoable
has_many :audios, :as => :audioable
has_many :images, :as => :imageable, :dependent => :nullify
has_many :videos, :as => :videoable, :dependent => :destroy
has_many :audios, :as => :audioable, :dependent => :destroy

accepts_nested_attributes_for :images,
:reject_if => proc { |attrs| attrs.all? { |k,v| v.blank? } }
accepts_nested_attributes_for :videos,
:reject_if => proc { |attrs| attrs.all? { |k,v| v.blank? } }
accepts_nested_attributes_for :audios,
:reject_if => proc { |attrs| attrs.all? { |k,v| v.blank? } }

include Newscloud::Acts::MediaItem::InstanceMethods
end
Expand Down

0 comments on commit 24f0864

Please sign in to comment.