Skip to content

Commit

Permalink
Merge pull request #183 from fsek/contact-fix
Browse files Browse the repository at this point in the history
Fixing News and Contact.
  • Loading branch information
davidwessman committed May 13, 2015
2 parents 9f7c13c + b2c5410 commit d92d346
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
11 changes: 5 additions & 6 deletions app/controllers/news_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class NewsController < ApplicationController
load_and_authorize_resource

def index
@news = News.all_date
end

def show
Expand All @@ -18,24 +19,22 @@ def edit
def create
@news.user = current_user
@news.save!
redirect_to @news
redirect_to news_path(@news)
end

def update
@news.update! news_params
redirect_to @news
redirect_to news_path(@news)
end

def destroy
@news.destroy!
redirect_to News
redirect_to news_index_path
end

private

def news_params
params.require(:news).permit(
:title, :content, :image,
)
params.require(:news).permit(:title, :content, :image)
end
end
4 changes: 3 additions & 1 deletion app/models/news.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ class News < ActiveRecord::Base
belongs_to :user

has_attached_file :image,
styles: {original: '4000x4000>', large: '800x800>', small: '250x250>', thumb: '100x100>'},
styles: { original: '4000x4000>', large: '800x800>',
small: '250x250>', thumb: '100x100>' },
path: ':rails_root/public/system/images/news/:id/:style/:filename',
url: '/system/images/news/:id/:style/:filename'

Expand All @@ -16,4 +17,5 @@ class News < ActiveRecord::Base
scope :not_removed, -> { where('d_remove > ?', Time.zone.today) }
scope :public_n, -> { where(public: true) }
scope :latest, -> { order(created_at: :asc).limit(5) }
scope :all_date, -> { order(created_at: :asc) }
end
2 changes: 1 addition & 1 deletion app/views/contact_mailer/contact_email.text.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ Från: <%=@name%>
E-post: <%=@email%>

Meddelande:
<%=@msg%>
<%=simple_format(@msg)%>
51 changes: 51 additions & 0 deletions spec/controllers/news_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'rails_helper'

RSpec.describe NewsController, type: :controller do
let(:user) { create(:user) }
let(:news) { create(:news, user: user) }

allow_user_to(:manage, News)
before(:each) do
allow(controller).to receive(:current_user) { user }
end
describe 'GET #show' do
it 'assigns the requested news as @news' do
get(:show, id: news.to_param)
assigns(:news).should eq(news)
end
end

describe 'GET #new' do
it 'assigns a new news as @news' do
get(:new)
assigns(:news).new_record?.should be_truthy
assigns(:news).instance_of?(News).should be_truthy
end
end

describe 'GET #index' do
it 'assigns news sorted as @news' do
get(:index)
assigns(:news).should eq(News.all.order(created_at: :asc))
end
end

describe 'POST #create' do
it 'posts new news' do
lambda do
post :create, news: attributes_for(:news)
end.should change(News, :count).by(1)

response.should redirect_to(News.last)
end
end

describe 'PATCH #update' do
it 'update news' do
patch :update, id: news.to_param, news: { title: 'Hej' }
news.reload
news.title.should eq('Hej')
response.should redirect_to(news)
end
end
end

0 comments on commit d92d346

Please sign in to comment.