Skip to content
This repository was archived by the owner on Nov 6, 2021. It is now read-only.
45 changes: 45 additions & 0 deletions app/controllers/impact_stories_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class ImpactStoriesController < ApplicationController
before_action :authenticate_user!

def index
@impact_stories = current_partner.impact_stories.sort_by(&:created_at).reverse
end

def show
@impact_story = current_partner.impact_stories.find(params[:id])
end

def new
@impact_story = current_partner.impact_stories.new
end

def create
@impact_story = current_partner.impact_stories.new(impact_story_params)

if @impact_story.save
redirect_to @impact_story, notice: "Impact Story was successfully created."
else
render :new
end
end

def edit
@impact_story = current_partner.impact_stories.find(params[:id])
end

def update
@impact_story = current_partner.impact_stories.find(params[:id])

if @impact_story.update(impact_story_params)
redirect_to @impact_story, notice: "Impact Story was successfully updated."
else
render :edit
end
end

private

def impact_story_params
params.require(:impact_story).permit(:title, :content)
end
end
21 changes: 21 additions & 0 deletions app/models/impact_story.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# == Schema Information
#
# Table name: impact_stories
#
# id :bigint(8) not null, primary key
# partner_id :bigint(8)
# title :string not null
# content :text not null
# created_at :datetime not null
# updated_at :datetime not null
#

class ImpactStory < ApplicationRecord
belongs_to :partner

validates :title, :content, presence: true

def blurb(limit)
content.truncate(limit, separator: ' ')
end
end
2 changes: 2 additions & 0 deletions app/models/partner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class Partner < ApplicationRecord
has_many :children, through: :families
has_many :authorized_family_members, through: :families

has_many :impact_stories, dependent: :destroy

has_many :partner_requests, dependent: :destroy
has_many :family_requests, dependent: :destroy
has_one :partner_form, primary_key: :diaper_bank_id, foreign_key: :diaper_bank_id, dependent: :destroy
Expand Down
28 changes: 28 additions & 0 deletions app/views/impact_stories/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<section class="content">
<div class="container-fluid">
<div class="row">
<!-- left column -->
<div class="col-md-12">
<!-- jquery validation -->
<div class="card card-primary">
<!-- /.card-header -->
<!-- form start -->
<div class="card-body">
<%= simple_form_for impact_story, html: {role: 'form', class: 'form-horizontal'} do |f| %>
<%= f.input :title, label: "Story Title", class: "form-control", wrapper: :input_group %>

<%= f.text_area :content, label: "Story Content", class: "form-control", wrapper: :input_group %>

<div class="card-footer">
<%= f.submit(class: 'btn btn-primary') %>
</div>
<% end %>
</div>
<!-- /.card -->
</div>
<p>We're working on adding image upload. Watch this space for updates!</p>
</div>
</div>
<!-- /.row -->
</div><!-- /.container-fluid -->
</section>
21 changes: 21 additions & 0 deletions app/views/impact_stories/_list.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<table class="table table-striped">
<thead>
<tr>
<th scope="col">Story Title</th>
<th scope="col">Date Created</th>
<th scope="col">Content</th>
<th scope="col" colspan="2">
</th>
</tr>
</thead>
<tbody>
<% @impact_stories.each do |story| %>
<tr>
<td><%= story.title %></td>
<td><%= story.created_at.strftime("%B %-d %Y") %></td>
<td><%= story.blurb(40) %></td>
<td><%= link_to 'View Story', story, class: "btn btn-sm btn-info pull-right" %></td>
<td><%= link_to 'Edit Story Details', edit_impact_story_path(story), class: "btn btn-sm btn-success pull-right" %></td>
<% end %>
</tbody>
</table>
27 changes: 27 additions & 0 deletions app/views/impact_stories/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<section class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<% content_for :title, "Impact Story - #{current_partner.name}" %>
<h1><i class="fa fa-user-plus"></i>&nbsp;&nbsp;
Edit Impact Story
<small>for <%= current_partner.name %></small>
</h1>
</div>
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item"><%= link_to(dashboard_path) do %>
<i class="fa fa-home fa-lg"></i> Home
<% end %>
</li>
<li class="breadcrumb-item"><a href="<%= impact_stories_path %>">All Impact Stories</a></li>
<li class="breadcrumb-item">
<a href="<%= impact_story_path(@impact_story) %>">The <%= "#{@impact_story.title}" %> Story</a></li>
<li class="breadcrumb-item"><a href="#">Edit</a></li>
</ol>
</div>
</div>
</div><!-- /.container-fluid -->
</section>

<%= render 'form', impact_story: @impact_story %>
64 changes: 64 additions & 0 deletions app/views/impact_stories/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<!-- Content Header (Page header) -->
<section class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<% content_for :title, "Impact Stories - #{current_partner.name}" %>
<h1><i class="fa fa-users"></i>&nbsp;&nbsp;
Impact Stories
<small>for <%= current_partner.name %></small>
</h1>
</div>
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item"><%= link_to(dashboard_path) do %>
<i class="fa fa-dashboard"></i> Home
<% end %>
</li>
<li class="breadcrumb-item"><a href="#">Impact Stories</a></li>
</ol>
</div>
</div>
</div><!-- /.container-fluid -->
</section>

<section class="content">
<div class="container-fluid">
<div class="row">
<!-- left column -->
<div class="col-md-12">
<!-- jquery validation -->
<div class="card">
<div class="card-footer">
<span class="float-right">
<%= link_to 'Add New Impact Story', new_impact_story_path, class: "btn btn-info" %>
</span>
</div>
</div>
<!-- /.card -->
</div>
<!--/.col (left) -->
</div>
<!-- /.row -->
</div><!-- /.container-fluid -->
</section>
<section class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<!-- Default box -->
<div class="card">
<div class="card-body">
<div id="filterrific_results">
<%= render(
partial: 'impact_stories/list',
locals: {impact_stories: @impact_stories}
) %>
</div>
</div>
</div>
<!-- /.card -->
</div>
</div>
</div>
</section>
25 changes: 25 additions & 0 deletions app/views/impact_stories/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<section class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<% content_for :title, "Impact Story - #{current_partner.name}" %>
<h1><i class="fa fa-user-plus"></i>&nbsp;&nbsp;
New Impact Story
<small>for <%= current_partner.name %></small>
</h1>
</div>
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item"><%= link_to(dashboard_path) do %>
<i class="fa fa-home fa-lg"></i> Home
<% end %>
</li>
<li class="breadcrumb-item"><a href="<%= impact_stories_path %>">All Impact Stories</a></li>
<li class="breadcrumb-item"><a href="#">New Impact Story</a></li>
</ol>
</div>
</div>
</div><!-- /.container-fluid -->
</section>

<%= render 'form', impact_story: @impact_story %>
49 changes: 49 additions & 0 deletions app/views/impact_stories/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<section class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<% content_for :title, "Impact Stories - #{current_partner.name}" %>
<h1><i class="fa fa-users"></i>&nbsp;&nbsp;
Impact Story Details
<small>for <%= current_partner.name %></small>
</h1>
</div>
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item"><%= link_to(dashboard_path) do %>
<i class="fa fa-home fa-lg"></i> Home
<% end %>
</li>
<li class="breadcrumb-item"><a href="<%= impact_stories_path %>">All Impact Stories</a></li>
<li class="breadcrumb-item"><a href="#">The <%= "#{@impact_story.title}" %> Story</a></li>
</ol>
</div>
</div>
</div><!-- /.container-fluid -->
</section>

<section class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="card mb-4">
<h5 class="card-header bg-primary text-white">Story Information
</h5>
<div class="card-body">
<dl>
<dt>Story Title:</dt>
<dd><%= @impact_story.title %></dd>

<dt>Story Content:</dt>
<dd><%= simple_format(h(@impact_story.content)) %></dd>
</dl>
</div>
<div class="card-footer">
<%= link_to 'Edit This Story', edit_impact_story_path(@impact_story), class: "btn btn-sm btn-primary" %>
<%= link_to 'Back to All Stories', impact_stories_path, class: "btn btn-sm btn-info pull-right" %>
</div>
</div>
</div>
</div>
</div>
</section>
6 changes: 6 additions & 0 deletions app/views/layouts/_sidebar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,11 @@
<% end %>
</li>

<li class="nav-item <%= 'active' if current_page?(impact_stories_path) %>">
<%= link_to(impact_stories_path, class: "nav-link #{"active" if current_page?(impact_stories_path)}") do %>
<i class="nav-icon fa fa-child"></i>
<p>Share Our Impact</p>
<% end %>
</li>

</ul>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
end
end

resources :impact_stories, except: [:destroy]

# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
get "pages/:name", to: "static#page", as: "static_page"
root "static#index"
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20200905195722_create_impact_stories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateImpactStories < ActiveRecord::Migration[6.0]
def change
create_table :impact_stories do |t|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add some database constraints to prevent it at the DB level from having incorrect data. I usually think about the database first when it comes to adding new models. Without these constraints, you could pass invalid data through if you bypass the callbacks using something like .update_column

t.string :title, null: false
t.text :content, null: false
t.references :partner, index: true, foreign_key: true

t.timestamps
end
end
end
12 changes: 11 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_05_18_010905) do
ActiveRecord::Schema.define(version: 2020_09_05_195722) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -119,6 +119,15 @@
t.index ["feature_key", "key", "value"], name: "index_flipper_gates_on_feature_key_and_key_and_value", unique: true
end

create_table "impact_stories", force: :cascade do |t|
t.string "title", null: false
t.text "content", null: false
t.bigint "partner_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["partner_id"], name: "index_impact_stories_on_partner_id"
end

create_table "item_requests", force: :cascade do |t|
t.string "name"
t.string "quantity"
Expand Down Expand Up @@ -273,6 +282,7 @@
add_foreign_key "child_item_requests", "item_requests"
add_foreign_key "children", "families"
add_foreign_key "families", "partners"
add_foreign_key "impact_stories", "partners"
add_foreign_key "item_requests", "partner_requests"
add_foreign_key "users", "partners"
end
19 changes: 19 additions & 0 deletions spec/factories/impact_stories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# == Schema Information
#
# Table name: impact_stories
#
# id :bigint(8) not null, primary key
# partner_id :bigint(8)
# title :string not null
# content :text not null
# created_at :datetime not null
# updated_at :datetime not null
#

FactoryBot.define do
factory :impact_story do
title { Faker::Lorem.word }
content { Faker::Lorem.paragraph }
partner
end
end
Loading