diff --git a/app/controllers/debates_controller.rb b/app/controllers/debates_controller.rb index 53545887e..718772015 100644 --- a/app/controllers/debates_controller.rb +++ b/app/controllers/debates_controller.rb @@ -1,6 +1,7 @@ class DebatesController < ApplicationController before_action :set_debate, only: [:show, :edit, :update] - + before_action :authenticate_user!, only: [:new, :create] + def index @debates = Debate.all end @@ -16,7 +17,9 @@ class DebatesController < ApplicationController end def create - @debate = Debate.create(debate_params) + @debate = Debate.new(debate_params) + @debate.author = current_user + @debate.save respond_with @debate end diff --git a/app/models/debate.rb b/app/models/debate.rb index c66913c59..22e15fe3e 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -1,6 +1,9 @@ class Debate < ActiveRecord::Base + belongs_to :author, class_name: 'User', foreign_key: 'author_id' + validates :title, presence: true validates :description, presence: true + validates :author, presence: true validates :terms_of_service, acceptance: { allow_nil: false }, on: :create -end +end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index c8220270d..9c55bc59e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,6 +1,8 @@ class User < ActiveRecord::Base - # Include default devise modules. Others available are: - # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable + + def name + "#{first_name} #{last_name}" + end end diff --git a/app/views/debates/_debate.html.erb b/app/views/debates/_debate.html.erb index 4f99c9df9..637c0086c 100644 --- a/app/views/debates/_debate.html.erb +++ b/app/views/debates/_debate.html.erb @@ -1,6 +1,9 @@

<%= link_to debate.title, debate %>

<%= debate.description %>

-

Creado el: <%= l debate.created_at.to_date %>

+

+ Creado el: <%= l debate.created_at.to_date %> + por: <%= debate.author.name %> +



\ No newline at end of file diff --git a/app/views/debates/_form.html.erb b/app/views/debates/_form.html.erb index 98de3c538..4ece98d8f 100644 --- a/app/views/debates/_form.html.erb +++ b/app/views/debates/_form.html.erb @@ -20,7 +20,7 @@

Explica con todo el detalle que puedas y de una manera sencilla la idea y que crees que conseguirĂ­amos con ella

<%= f.text_area :description %> - <% if action_name == 'new' %> + <% if @debate.new_record? %> <%= f.check_box :terms_of_service %> Acepto la polĂ­tica de privacidad y el aviso legal <% end %> diff --git a/app/views/debates/show.html.erb b/app/views/debates/show.html.erb index 777b22d1b..f0a67e527 100644 --- a/app/views/debates/show.html.erb +++ b/app/views/debates/show.html.erb @@ -1,7 +1,10 @@

<%= @debate.title %>

<%= @debate.description %>

-

Creado el: <%= l @debate.created_at.to_date %>

+

+ Creado el: <%= l @debate.created_at.to_date %> + por: <%= @debate.author.name %> +

<%= link_to 'Edit', edit_debate_path(@debate) %> | diff --git a/spec/factories.rb b/spec/factories.rb index 971791b0b..0433e2b15 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -11,6 +11,7 @@ FactoryGirl.define do title 'Debate title' description 'Debate description' terms_of_service '1' + association :author, factory: :user end end \ No newline at end of file diff --git a/spec/features/debates_spec.rb b/spec/features/debates_spec.rb index 48f7d3b62..ecc4892af 100644 --- a/spec/features/debates_spec.rb +++ b/spec/features/debates_spec.rb @@ -12,6 +12,7 @@ feature 'Debates' do expect(page).to have_content "Debate title" expect(page).to have_content "Debate description" expect(page).to have_content "Creado el: #{I18n.l Date.today}" + expect(page).to have_content "por: #{Debate.first.author.name}" end end @@ -23,9 +24,13 @@ feature 'Debates' do expect(page).to have_content "Debate title" expect(page).to have_content "Debate description" expect(page).to have_content "Creado el: #{I18n.l Date.today}" + expect(page).to have_content "por: #{debate.author.name}" end scenario 'Create' do + author = create(:user) + login_as(author) + visit new_debate_path fill_in 'debate_title', with: 'Acabar con los desahucios' fill_in 'debate_description', with: 'Esto es un tema muy importante porque...' @@ -36,6 +41,8 @@ feature 'Debates' do expect(page).to have_content 'Debate creado correctamente' expect(page).to have_content 'Acabar con los desahucios' expect(page).to have_content 'Esto es un tema muy importante porque...' + expect(page).to have_content "Creado el: #{I18n.l Date.today}" + expect(page).to have_content "por: #{author.name}" end scenario 'Update' do diff --git a/spec/models/debate_spec.rb b/spec/models/debate_spec.rb index 490eea985..f277f1d7b 100644 --- a/spec/models/debate_spec.rb +++ b/spec/models/debate_spec.rb @@ -10,6 +10,11 @@ describe Debate do expect(@debate).to be_valid end + it "should not be valid without an author" do + @debate.author = nil + expect(@debate).to_not be_valid + end + it "should not be valid without a title" do @debate.title = nil expect(@debate).to_not be_valid