-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40d98ac
commit 7168522
Showing
17 changed files
with
215 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
class NewsReleasesController < ApplicationController | ||
before_action :authenticate, except: [:index, :show] | ||
before_action :set_news_release, only: [:show, :edit, :update, :destroy] | ||
|
||
# GET /news_releases | ||
# GET /news_releases.json | ||
def index | ||
@news_releases = NewsRelease.all | ||
end | ||
|
||
# GET /news_releases/1 | ||
# GET /news_releases/1.json | ||
def show | ||
end | ||
|
||
# GET /news_releases/new | ||
def new | ||
@news_release = NewsRelease.new | ||
end | ||
|
||
# GET /news_releases/1/edit | ||
def edit | ||
end | ||
|
||
# POST /news_releases | ||
# POST /news_releases.json | ||
def create | ||
@news_release = NewsRelease.new(news_release_params) | ||
|
||
respond_to do |format| | ||
if @news_release.save | ||
format.html { redirect_to news_releases_url, | ||
notice: 'Successfully created news release.' } | ||
format.json { render action: 'show', status: :created, location: @news_release } | ||
else | ||
format.html { render action: 'new' } | ||
format.json { render json: @news_release.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# PATCH/PUT /news_releases/1 | ||
# PATCH/PUT /news_releases/1.json | ||
def update | ||
respond_to do |format| | ||
if @news_release.update(news_release_params) | ||
format.html { redirect_to @news_release, notice: 'News release was successfully updated.' } | ||
format.json { head :no_content } | ||
else | ||
format.html { render action: 'edit' } | ||
format.json { render json: @news_release.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# DELETE /news_releases/1 | ||
# DELETE /news_releases/1.json | ||
def destroy | ||
@news_release.destroy | ||
respond_to do |format| | ||
format.html { redirect_to news_releases_url } | ||
format.json { head :no_content } | ||
end | ||
end | ||
|
||
private | ||
# Use callbacks to share common setup or constraints between actions. | ||
def set_news_release | ||
@news_release = NewsRelease.find(params[:id]) | ||
end | ||
|
||
# Never trust parameters from the scary internet, only allow the white list through. | ||
def news_release_params | ||
params.require(:news_release).permit(:title, :released_on, :body) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class NewsRelease < ActiveRecord::Base | ||
validates :released_on, presence: true | ||
validates :title, presence: true | ||
validates :body, presence: true | ||
|
||
def title_with_date | ||
"#{released_on.strftime('%Y-%m-%d')}: #{title}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<%= form_for(@news_release) do |f| %> | ||
<div class="form-inputs"> | ||
<div class="field"> | ||
<%= f.label :title %><br> | ||
<%= f.text_field :title %> | ||
</div> | ||
<div class="field"> | ||
<%= f.label :released_on, 'Date' %><br> | ||
<%= f.text_field :released_on %> | ||
</div> | ||
<div class="field"> | ||
<%= f.label :body %><br> | ||
<%= f.text_area :body %> | ||
</div> | ||
</div> | ||
<div class="form-actions"> | ||
<%= f.submit nil, class: 'btn btn-primary' %> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<h1>Editing news_release</h1> | ||
|
||
<%= render 'form' %> | ||
|
||
<%= link_to 'Show', @news_release %> | | ||
<%= link_to 'Back', news_releases_path %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<h1>News releases</h1> | ||
|
||
<ul> | ||
<% @news_releases.each do |news_release| %> | ||
<li> | ||
<%= link_to news_release.title_with_date, news_release %> | ||
</li> | ||
<% end %> | ||
</ul> | ||
|
||
<p> | ||
<%= link_to 'Add News Release', new_news_release_path, class: 'btn btn-primary' %> | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
json.array!(@news_releases) do |news_release| | ||
json.extract! news_release, :title, :released_on, :body | ||
json.url news_release_url(news_release, format: :json) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<h1>New news release</h1> | ||
|
||
<%= render 'form' %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<p id="notice"><%= notice %></p> | ||
|
||
<p> | ||
<strong>Title:</strong> | ||
<%= @news_release.title %> | ||
</p> | ||
|
||
<p> | ||
<strong>Released on:</strong> | ||
<%= @news_release.released_on %> | ||
</p> | ||
|
||
<p> | ||
<strong>Body:</strong> | ||
<%= @news_release.body %> | ||
</p> | ||
|
||
<%= link_to 'Edit', edit_news_release_path(@news_release) %> | | ||
<%= link_to 'Back', news_releases_path %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.extract! @news_release, :title, :released_on, :body, :created_at, :updated_at |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class CreateNewsReleases < ActiveRecord::Migration | ||
def change | ||
create_table :news_releases do |t| | ||
t.string :title | ||
t.date :released_on | ||
t.text :body | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require 'spec_helper' | ||
|
||
describe NewsReleasesController do | ||
describe 'GET #new' do | ||
it "requires login" do | ||
get :new | ||
expect(response).to require_login | ||
end | ||
end | ||
|
||
describe "POST #create" do | ||
it "requires login" do | ||
post :create, news_release: attributes_for(:news_release) | ||
expect(response).to require_login | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
require 'faker' | ||
|
||
FactoryGirl.define do | ||
factory :news_release do | ||
title "Test news release" | ||
released_on 1.day.ago | ||
body { Faker::Lorem.paragraph } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require 'spec_helper' | ||
|
||
describe NewsRelease do | ||
it { should validate_presence_of :released_on } | ||
it { should validate_presence_of :title } | ||
it { should validate_presence_of :body } | ||
|
||
it "returns the formatted date and title as a string" do | ||
news_release = NewsRelease.new( | ||
released_on: '2013-07-31', | ||
title: 'BigCo hires new CEO') | ||
expect(news_release.title_with_date).to eq '2013-07-31: BigCo hires new CEO' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters