Skip to content

Commit

Permalink
Move contributor to post model
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwindt committed Mar 8, 2012
1 parent 29c3ce6 commit 24c7c89
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 12 deletions.
1 change: 0 additions & 1 deletion app/models/medium.rb
@@ -1,6 +1,5 @@
class Medium < ActiveRecord::Base
mount_uploader :asset, MediaUploader

belongs_to :contributor, :class_name => 'User'
validates :asset, :presence => true
end
5 changes: 2 additions & 3 deletions app/models/post.rb
Expand Up @@ -7,6 +7,7 @@ class Post < ActiveRecord::Base
has_many :comments, :dependent => :destroy
has_many :audios, :dependent => :destroy
has_many :media, :dependent => :destroy
belongs_to :contributor, :class_name => 'User'
accepts_nested_attributes_for :media

attr_accessible :title, :content, :created_at, :media_attributes
Expand Down Expand Up @@ -77,9 +78,7 @@ def new_contribution(params, user)
post = new(params)
post.status = 'pending'
post.created_at += 4.hours # a las 4 de la mañana, para que no interfiera con los automáticos
post.media.each do |medium|
medium.contributor = user
end
post.contributor = user
# Agrega un media vacío si no hay ninguno, para que falle la validación
post.media << Medium.new if post.media.size == 0
post
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Expand Up @@ -2,7 +2,7 @@ class User < ActiveRecord::Base
extend FriendlyId
friendly_id :alias, :use => :slugged
has_many :comments, :dependent => :nullify
has_many :contributions, :class_name => 'Medium', :foreign_key => :contributor_id, :dependent => :nullify
has_many :contributions, :class_name => 'Post', :foreign_key => :contributor_id, :dependent => :nullify
validates :alias, :presence => true, :uniqueness => { :case_sensitive => false }
before_save :update_gravatar_hash, :clean_role
delegate :can?, :cannot?, :to => :ability
Expand Down
2 changes: 1 addition & 1 deletion app/views/post_mailer/new_contribution.html.haml
Expand Up @@ -5,7 +5,7 @@

%body
%p
Hay una nueva contribución para moderar:
Hay una nueva contribución de #{link_to @post.contributor.alias, user_url(@post.contributor)} para moderar:
%p
= link_to @post.title, post_url(@post)
%p
Expand Down
2 changes: 1 addition & 1 deletion app/views/post_mailer/new_contribution.text.haml
@@ -1,4 +1,4 @@
Hay una nueva contribución para moderar:
Hay una nueva contribución de #{user_url(@post.contributor)} para moderar:

Título: #{@post.title}
URL: #{post_url(@post)}
Expand Down
2 changes: 1 addition & 1 deletion app/views/posts/_post.html.haml
Expand Up @@ -26,4 +26,4 @@
- if can?(:update, Post) && post.pending?
%p.meta
= link_to "Aprobar contribución", approve_contribution_admin_post_path(post)
(Enviado por #{link_to post.media.first.contributor.alias, post.media.first.contributor})
(Enviado por #{link_to post.contributor.alias, post.contributor})
6 changes: 6 additions & 0 deletions db/migrate/20120308021742_move_contribution_to_posts.rb
@@ -0,0 +1,6 @@
class MoveContributionToPosts < ActiveRecord::Migration
def change
remove_column :media, :contributor_id
add_column :posts, :contributor_id, :integer
end
end
8 changes: 4 additions & 4 deletions db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20120229020055) do
ActiveRecord::Schema.define(:version => 20120308021742) do

create_table "articles", :force => true do |t|
t.string "title"
Expand Down Expand Up @@ -51,10 +51,9 @@
t.string "name"
t.string "description"
t.string "asset"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "post_id"
t.integer "contributor_id"
end

create_table "posts", :force => true do |t|
Expand All @@ -65,6 +64,7 @@
t.string "slug"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "contributor_id"
end

add_index "posts", ["created_at"], :name => "index_posts_on_created_at"
Expand Down

0 comments on commit 24c7c89

Please sign in to comment.