From 3ff95d09e644b2e76bff28c6895a85624bb8f11c Mon Sep 17 00:00:00 2001 From: fosterv2 Date: Sun, 6 Sep 2020 11:18:21 -0700 Subject: [PATCH 01/11] Generate files for impact_stories model. Added all necessary views and controller actions. --- app/assets/stylesheets/impact_stories.scss | 3 + app/controllers/impact_stories_controller.rb | 45 ++++++++++++++ app/helpers/impact_stories_helper.rb | 2 + app/models/impact_story.rb | 3 + app/models/partner.rb | 2 + app/views/impact_stories/_form.html.erb | 27 ++++++++ app/views/impact_stories/_list.html.erb | 22 +++++++ app/views/impact_stories/edit.html.erb | 27 ++++++++ app/views/impact_stories/index.html.erb | 62 +++++++++++++++++++ app/views/impact_stories/new.html.erb | 25 ++++++++ app/views/impact_stories/show.html.erb | 49 +++++++++++++++ app/views/layouts/_sidebar.html.erb | 6 ++ config/routes.rb | 1 + .../20200905195722_create_impact_stories.rb | 11 ++++ db/schema.rb | 10 ++- spec/factories/impact_stories.rb | 6 ++ spec/helpers/impact_stories_helper_spec.rb | 15 +++++ spec/models/impact_story_spec.rb | 5 ++ spec/requests/impact_stories_request_spec.rb | 5 ++ 19 files changed, 325 insertions(+), 1 deletion(-) create mode 100644 app/assets/stylesheets/impact_stories.scss create mode 100644 app/controllers/impact_stories_controller.rb create mode 100644 app/helpers/impact_stories_helper.rb create mode 100644 app/models/impact_story.rb create mode 100644 app/views/impact_stories/_form.html.erb create mode 100644 app/views/impact_stories/_list.html.erb create mode 100644 app/views/impact_stories/edit.html.erb create mode 100644 app/views/impact_stories/index.html.erb create mode 100644 app/views/impact_stories/new.html.erb create mode 100644 app/views/impact_stories/show.html.erb create mode 100644 db/migrate/20200905195722_create_impact_stories.rb create mode 100644 spec/factories/impact_stories.rb create mode 100644 spec/helpers/impact_stories_helper_spec.rb create mode 100644 spec/models/impact_story_spec.rb create mode 100644 spec/requests/impact_stories_request_spec.rb diff --git a/app/assets/stylesheets/impact_stories.scss b/app/assets/stylesheets/impact_stories.scss new file mode 100644 index 00000000..27752b40 --- /dev/null +++ b/app/assets/stylesheets/impact_stories.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the ImpactStories controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: https://sass-lang.com/ diff --git a/app/controllers/impact_stories_controller.rb b/app/controllers/impact_stories_controller.rb new file mode 100644 index 00000000..e6f2b19c --- /dev/null +++ b/app/controllers/impact_stories_controller.rb @@ -0,0 +1,45 @@ +class ImpactStoriesController < ApplicationController + before_action :authenticate_user! + + def index + @impact_stories = current_partner.impact_stories + 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 diff --git a/app/helpers/impact_stories_helper.rb b/app/helpers/impact_stories_helper.rb new file mode 100644 index 00000000..5c164c70 --- /dev/null +++ b/app/helpers/impact_stories_helper.rb @@ -0,0 +1,2 @@ +module ImpactStoriesHelper +end diff --git a/app/models/impact_story.rb b/app/models/impact_story.rb new file mode 100644 index 00000000..4be43e9c --- /dev/null +++ b/app/models/impact_story.rb @@ -0,0 +1,3 @@ +class ImpactStory < ApplicationRecord + belongs_to :partner +end diff --git a/app/models/partner.rb b/app/models/partner.rb index 5272d70f..9d0bd9b8 100644 --- a/app/models/partner.rb +++ b/app/models/partner.rb @@ -99,6 +99,8 @@ class Partner < ApplicationRecord has_many :children, through: :families has_many :authorized_family_members, through: :families + has_many :impact_stories + 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 diff --git a/app/views/impact_stories/_form.html.erb b/app/views/impact_stories/_form.html.erb new file mode 100644 index 00000000..2cd7f0b2 --- /dev/null +++ b/app/views/impact_stories/_form.html.erb @@ -0,0 +1,27 @@ +
+
+
+ +
+ +
+ + +
+ <%= 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 %> + + + <% end %> +
+ +
+
+
+ +
+
\ No newline at end of file diff --git a/app/views/impact_stories/_list.html.erb b/app/views/impact_stories/_list.html.erb new file mode 100644 index 00000000..8d4b475d --- /dev/null +++ b/app/views/impact_stories/_list.html.erb @@ -0,0 +1,22 @@ + + + + + + + + + + + <% @impact_stories.each do |story| %> + + + + + + + <% end %> + +
Story TitleDate CreatedContent + <%# link_to 'Export Results To CSV', families_path(nil, format: :csv), class: "btn btn-info pull-right" %> +
<%= story.title %><%= story.created_at %><%= story.content %><%= link_to 'View Story', story, class: "btn btn-sm btn-info pull-right" %><%= link_to 'Edit Story Details', edit_impact_story_path(story), class: "btn btn-sm btn-success pull-right" %>
diff --git a/app/views/impact_stories/edit.html.erb b/app/views/impact_stories/edit.html.erb new file mode 100644 index 00000000..bc9bfa67 --- /dev/null +++ b/app/views/impact_stories/edit.html.erb @@ -0,0 +1,27 @@ +
+
+
+
+ <% content_for :title, "Impact Story - #{current_partner.name}" %> +

   + Edit Impact Story + for <%= current_partner.name %> +

+
+
+ +
+
+
+
+ +<%= render 'form', impact_story: @impact_story %> \ No newline at end of file diff --git a/app/views/impact_stories/index.html.erb b/app/views/impact_stories/index.html.erb new file mode 100644 index 00000000..9262a2c3 --- /dev/null +++ b/app/views/impact_stories/index.html.erb @@ -0,0 +1,62 @@ + +
+
+
+
+ <% content_for :title, "Impact Stories - #{current_partner.name}" %> +

   + Impact Stories + for <%= current_partner.name %> +

+
+
+ +
+
+
+
+ +
+
+
+ +
+ +
+ + <%= link_to 'Add New Impact Story', new_impact_story_path, class: "btn btn-info" %> + +
+ +
+ +
+ +
+
+
+
+
+
+ +
+
+
+ <%= render( + partial: 'impact_stories/list', + locals: {impact_stories: @impact_stories} + ) %> +
+
+
+ +
+
+
+
diff --git a/app/views/impact_stories/new.html.erb b/app/views/impact_stories/new.html.erb new file mode 100644 index 00000000..e99f3a91 --- /dev/null +++ b/app/views/impact_stories/new.html.erb @@ -0,0 +1,25 @@ +
+
+
+
+ <% content_for :title, "Impact Story - #{current_partner.name}" %> +

   + New Impact Story + for <%= current_partner.name %> +

+
+
+ +
+
+
+
+ +<%= render 'form', impact_story: @impact_story %> \ No newline at end of file diff --git a/app/views/impact_stories/show.html.erb b/app/views/impact_stories/show.html.erb new file mode 100644 index 00000000..133ab3cd --- /dev/null +++ b/app/views/impact_stories/show.html.erb @@ -0,0 +1,49 @@ +
+
+
+
+ <% content_for :title, "Impact Stories - #{current_partner.name}" %> +

   + Impact Story Details + for <%= current_partner.name %> +

+
+
+ +
+
+
+
+ +
+
+
+
+
+
Story Information +
+
+
+
Story Title:
+
<%= @impact_story.title %>
+ +
Story Content:
+
<%= @impact_story.content %>
+
+
+ +
+
+
+
+
diff --git a/app/views/layouts/_sidebar.html.erb b/app/views/layouts/_sidebar.html.erb index 61ce9690..aaf6f7a9 100644 --- a/app/views/layouts/_sidebar.html.erb +++ b/app/views/layouts/_sidebar.html.erb @@ -41,5 +41,11 @@ <% end %> + \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index d3ff5a2e..b85bf50d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + resources :impact_stories flipper_app = Flipper::UI.app(Flipper.instance) do |builder| builder.use Rack::Auth::Basic do |username, password| username == ENV["FLIPPER_USERNAME"] && password == ENV["FLIPPER_PASSWORD"] diff --git a/db/migrate/20200905195722_create_impact_stories.rb b/db/migrate/20200905195722_create_impact_stories.rb new file mode 100644 index 00000000..99830caa --- /dev/null +++ b/db/migrate/20200905195722_create_impact_stories.rb @@ -0,0 +1,11 @@ +class CreateImpactStories < ActiveRecord::Migration[6.0] + def change + create_table :impact_stories do |t| + t.string :title + t.text :content + t.integer :partner_id + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 307fd9c3..c499b683 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -119,6 +119,14 @@ 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" + t.text "content" + t.integer "partner_id" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + end + create_table "item_requests", force: :cascade do |t| t.string "name" t.string "quantity" diff --git a/spec/factories/impact_stories.rb b/spec/factories/impact_stories.rb new file mode 100644 index 00000000..c0cf0148 --- /dev/null +++ b/spec/factories/impact_stories.rb @@ -0,0 +1,6 @@ +FactoryBot.define do + factory :impact_story do + title { "MyString" } + content { "MyText" } + end +end diff --git a/spec/helpers/impact_stories_helper_spec.rb b/spec/helpers/impact_stories_helper_spec.rb new file mode 100644 index 00000000..1ba35187 --- /dev/null +++ b/spec/helpers/impact_stories_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the ImpactStoriesHelper. For example: +# +# describe ImpactStoriesHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe ImpactStoriesHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/impact_story_spec.rb b/spec/models/impact_story_spec.rb new file mode 100644 index 00000000..fd76508c --- /dev/null +++ b/spec/models/impact_story_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe ImpactStory, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/requests/impact_stories_request_spec.rb b/spec/requests/impact_stories_request_spec.rb new file mode 100644 index 00000000..ec2a5a13 --- /dev/null +++ b/spec/requests/impact_stories_request_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe "ImpactStories", type: :request do + +end From 0b9959a416f978b67528a78ba435adec02197e58 Mon Sep 17 00:00:00 2001 From: fosterv2 Date: Sun, 6 Sep 2020 12:59:23 -0700 Subject: [PATCH 02/11] formatted all the views correctly. --- app/models/impact_story.rb | 4 ++++ app/views/impact_stories/_form.html.erb | 1 + app/views/impact_stories/_list.html.erb | 4 ++-- app/views/impact_stories/index.html.erb | 4 +++- app/views/impact_stories/show.html.erb | 2 +- config/routes.rb | 3 ++- 6 files changed, 13 insertions(+), 5 deletions(-) diff --git a/app/models/impact_story.rb b/app/models/impact_story.rb index 4be43e9c..70a7d3e3 100644 --- a/app/models/impact_story.rb +++ b/app/models/impact_story.rb @@ -1,3 +1,7 @@ class ImpactStory < ApplicationRecord belongs_to :partner + + def blurb(limit) + if content.length > limit then "#{content[0, limit]}…" else content end + end end diff --git a/app/views/impact_stories/_form.html.erb b/app/views/impact_stories/_form.html.erb index 2cd7f0b2..7462c58e 100644 --- a/app/views/impact_stories/_form.html.erb +++ b/app/views/impact_stories/_form.html.erb @@ -20,6 +20,7 @@ +

We're working on adding image upload. Watch this space for updates!

diff --git a/app/views/impact_stories/_list.html.erb b/app/views/impact_stories/_list.html.erb index 8d4b475d..04d6e494 100644 --- a/app/views/impact_stories/_list.html.erb +++ b/app/views/impact_stories/_list.html.erb @@ -13,8 +13,8 @@ <% @impact_stories.each do |story| %> <%= story.title %> - <%= story.created_at %> - <%= story.content %> + <%= story.created_at.strftime("%B %-d %Y") %> + <%= story.blurb(30) %> <%= link_to 'View Story', story, class: "btn btn-sm btn-info pull-right" %> <%= link_to 'Edit Story Details', edit_impact_story_path(story), class: "btn btn-sm btn-success pull-right" %> <% end %> diff --git a/app/views/impact_stories/index.html.erb b/app/views/impact_stories/index.html.erb index 9262a2c3..33277f72 100644 --- a/app/views/impact_stories/index.html.erb +++ b/app/views/impact_stories/index.html.erb @@ -29,9 +29,11 @@
+
diff --git a/app/views/impact_stories/show.html.erb b/app/views/impact_stories/show.html.erb index 133ab3cd..b4794d1c 100644 --- a/app/views/impact_stories/show.html.erb +++ b/app/views/impact_stories/show.html.erb @@ -35,7 +35,7 @@
<%= @impact_story.title %>
Story Content:
-
<%= @impact_story.content %>
+
<%= simple_format(h(@impact_story.content)) %>
diff --git a/app/views/impact_story_mailer/impact_story_email.html.erb b/app/views/impact_story_mailer/impact_story_email.html.erb new file mode 100644 index 00000000..b0cb920a --- /dev/null +++ b/app/views/impact_story_mailer/impact_story_email.html.erb @@ -0,0 +1,5 @@ + + +

<%= @impact_story.title %>

+ +

<%= @impact_story.content %>

diff --git a/config/environments/development.rb b/config/environments/development.rb index 9fc9fb32..7fbebb3e 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -42,6 +42,8 @@ config.action_mailer.perform_caching = false + config.action_mailer.preview_path = "#{Rails.root}/lib/previews" + # Print deprecation notices to the Rails logger. config.active_support.deprecation = :log diff --git a/config/routes.rb b/config/routes.rb index c6375cdf..37b03895 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -66,7 +66,11 @@ end end - resources :impact_stories + resources :impact_stories do + member do + post 'email' + end + end # 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" diff --git a/lib/previews/impact_story_mailer_preview.rb b/lib/previews/impact_story_mailer_preview.rb new file mode 100644 index 00000000..556f7259 --- /dev/null +++ b/lib/previews/impact_story_mailer_preview.rb @@ -0,0 +1,5 @@ +class ImpactStoryMailerPreview < ActionMailer::Preview + def impact_story_email + ImpactStoryMailer.impact_story_email(ImpactStory.first) + end +end diff --git a/spec/factories/impact_stories.rb b/spec/factories/impact_stories.rb index c0cf0148..a80ea781 100644 --- a/spec/factories/impact_stories.rb +++ b/spec/factories/impact_stories.rb @@ -1,6 +1,7 @@ FactoryBot.define do factory :impact_story do - title { "MyString" } + title { "MyTitle" } content { "MyText" } + partner end end diff --git a/spec/models/impact_story_spec.rb b/spec/models/impact_story_spec.rb index fd76508c..9ac260e2 100644 --- a/spec/models/impact_story_spec.rb +++ b/spec/models/impact_story_spec.rb @@ -1,5 +1,9 @@ require 'rails_helper' RSpec.describe ImpactStory, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + it "returns the first part of the content according to the given limit" do + impact_story = + create(:impact_story, title: "For Testing", content: "Making some long content that needs to be shortened according to a given limit") + expect("Making some long con…").to eq(impact_story.blurb(20)) + end end From 80580bfea50ae9d4d85bfa04af4bc88246f20c01 Mon Sep 17 00:00:00 2001 From: fosterv2 Date: Sun, 13 Sep 2020 07:07:45 -0700 Subject: [PATCH 04/11] Add impact story model, request, and feature specs --- app/controllers/impact_stories_controller.rb | 2 +- spec/features/impact_story_feature_spec.rb | 35 ++++++++++++++++ spec/requests/impact_stories_request_spec.rb | 43 ++++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 spec/features/impact_story_feature_spec.rb diff --git a/app/controllers/impact_stories_controller.rb b/app/controllers/impact_stories_controller.rb index 32f67201..39eb0a71 100644 --- a/app/controllers/impact_stories_controller.rb +++ b/app/controllers/impact_stories_controller.rb @@ -2,7 +2,7 @@ class ImpactStoriesController < ApplicationController before_action :authenticate_user! def index - @impact_stories = current_partner.impact_stories + @impact_stories = current_partner.impact_stories.sort_by { |story| story.created_at }.reverse end def show diff --git a/spec/features/impact_story_feature_spec.rb b/spec/features/impact_story_feature_spec.rb new file mode 100644 index 00000000..e3aac358 --- /dev/null +++ b/spec/features/impact_story_feature_spec.rb @@ -0,0 +1,35 @@ +require "rails_helper" + +describe ImpactStory, type: :feature, include_shared: true, js: true do + let(:partner) { create(:partner, :verified, id: 3) } + let(:user) { create(:user, partner: partner) } + + before do + Flipper[:impact_story_requests].enable(partner) + sign_in(user) + visit(root_path) + end + + scenario "User can see a list of impact stories" do + diaper_type = "Magic diaper" + stub_request(:any, "#{ENV["DIAPERBANK_ENDPOINT"]}/partner_requests/#{partner.id}") + .to_return(body: [{ id: 1, name: diaper_type }].to_json, status: 200) + + impact_stories = [ + create(:impact_story, title: "Title1", content: "Content 1", partner: partner), + create(:impact_story, title: "Title2", content: "Content 2", partner: partner) + ].reverse + + click_link "Share Our Impact" + impact_stories.each.with_index do |story, index| + within "tbody" do + expect(find("tr:nth-child(#{index + 1}) td:nth-child(1)")) + .to have_text(story.title) + expect(find("tr:nth-child(#{index + 1}) td:nth-child(2)")) + .to have_text(story.created_at.strftime("%B %-d %Y")) + expect(find("tr:nth-child(#{index + 1}) td:nth-child(3)")) + .to have_text(story.content) + end + end + end +end diff --git a/spec/requests/impact_stories_request_spec.rb b/spec/requests/impact_stories_request_spec.rb index ec2a5a13..58b47fea 100644 --- a/spec/requests/impact_stories_request_spec.rb +++ b/spec/requests/impact_stories_request_spec.rb @@ -1,5 +1,48 @@ require 'rails_helper' RSpec.describe "ImpactStories", type: :request do + let(:partner) { create(:partner) } + let!(:user) { create(:user, partner: partner) } + before do + sign_in user + end + + describe "GET #index" do + it "return http sucess" do + get impact_stories_path + + expect(response).to have_http_status(:ok) + end + end + + describe "GET #new" do + it "should return status code 200" do + get new_impact_story_path + + expect(response).to have_http_status :ok + end + end + + describe "POST #create" do + it "should create and redirect to impact_story_path" do + post impact_stories_path, params: { impact_story: attributes_for(:impact_story) } + + impact_story = ImpactStory.select(:id).last + + expect(response).to redirect_to(impact_story_path(impact_story.id)) + expect(request.flash[:notice]).to eql "Impact Story was successfully created." + end + end + + describe "PUT #update" do + let(:impact_story) { create(:impact_story, partner: partner) } + + it "should update and redirect to impact_story_path" do + put impact_story_path(impact_story), params: { impact_story: attributes_for(:impact_story) } + + expect(response).to redirect_to(impact_story_path(impact_story.id)) + expect(request.flash[:notice]).to eql "Impact Story was successfully updated." + end + end end From cab0733c6e22f5f014492f35e5943b9be145babd Mon Sep 17 00:00:00 2001 From: fosterv2 Date: Sun, 13 Sep 2020 07:42:59 -0700 Subject: [PATCH 05/11] Remove emailing framework --- app/mailers/impact_story_mailer.rb | 11 ----------- app/views/impact_stories/show.html.erb | 1 - .../impact_story_mailer/impact_story_email.html.erb | 5 ----- config/environments/development.rb | 2 -- config/routes.rb | 6 +----- lib/previews/impact_story_mailer_preview.rb | 5 ----- 6 files changed, 1 insertion(+), 29 deletions(-) delete mode 100644 app/mailers/impact_story_mailer.rb delete mode 100644 app/views/impact_story_mailer/impact_story_email.html.erb delete mode 100644 lib/previews/impact_story_mailer_preview.rb diff --git a/app/mailers/impact_story_mailer.rb b/app/mailers/impact_story_mailer.rb deleted file mode 100644 index e08f3515..00000000 --- a/app/mailers/impact_story_mailer.rb +++ /dev/null @@ -1,11 +0,0 @@ -class ImpactStoryMailer < ApplicationMailer - default from: "info@partner.app" - layout "mailer" - - def impact_story_email(impact_story) - @impact_story = impact_story - - mail to: "something@example.com", - subject: "New Impact Story From #{@impact_story.partner.name}" - end -end \ No newline at end of file diff --git a/app/views/impact_stories/show.html.erb b/app/views/impact_stories/show.html.erb index e93dc4a5..b4794d1c 100644 --- a/app/views/impact_stories/show.html.erb +++ b/app/views/impact_stories/show.html.erb @@ -39,7 +39,6 @@ diff --git a/app/views/impact_story_mailer/impact_story_email.html.erb b/app/views/impact_story_mailer/impact_story_email.html.erb deleted file mode 100644 index b0cb920a..00000000 --- a/app/views/impact_story_mailer/impact_story_email.html.erb +++ /dev/null @@ -1,5 +0,0 @@ - - -

<%= @impact_story.title %>

- -

<%= @impact_story.content %>

diff --git a/config/environments/development.rb b/config/environments/development.rb index 7fbebb3e..9fc9fb32 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -42,8 +42,6 @@ config.action_mailer.perform_caching = false - config.action_mailer.preview_path = "#{Rails.root}/lib/previews" - # Print deprecation notices to the Rails logger. config.active_support.deprecation = :log diff --git a/config/routes.rb b/config/routes.rb index 37b03895..c6375cdf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -66,11 +66,7 @@ end end - resources :impact_stories do - member do - post 'email' - end - end + resources :impact_stories # 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" diff --git a/lib/previews/impact_story_mailer_preview.rb b/lib/previews/impact_story_mailer_preview.rb deleted file mode 100644 index 556f7259..00000000 --- a/lib/previews/impact_story_mailer_preview.rb +++ /dev/null @@ -1,5 +0,0 @@ -class ImpactStoryMailerPreview < ActionMailer::Preview - def impact_story_email - ImpactStoryMailer.impact_story_email(ImpactStory.first) - end -end From dfd3e43a5c68cfbe73795d3dec7e0132c994a62b Mon Sep 17 00:00:00 2001 From: fosterv2 Date: Sun, 13 Sep 2020 08:19:40 -0700 Subject: [PATCH 06/11] Fix indentation in all files --- app/controllers/impact_stories_controller.rb | 66 +++++++++----------- app/models/impact_story.rb | 8 +-- app/models/partner.rb | 2 +- spec/features/impact_story_feature_spec.rb | 52 +++++++-------- spec/models/impact_story_spec.rb | 2 +- spec/requests/impact_stories_request_spec.rb | 58 ++++++++--------- 6 files changed, 92 insertions(+), 96 deletions(-) diff --git a/app/controllers/impact_stories_controller.rb b/app/controllers/impact_stories_controller.rb index 39eb0a71..8034c4b1 100644 --- a/app/controllers/impact_stories_controller.rb +++ b/app/controllers/impact_stories_controller.rb @@ -1,49 +1,45 @@ class ImpactStoriesController < ApplicationController - before_action :authenticate_user! + before_action :authenticate_user! - def index - @impact_stories = current_partner.impact_stories.sort_by { |story| story.created_at }.reverse - end + def index + @impact_stories = current_partner.impact_stories.sort_by { |story| story.created_at }.reverse + end - def show - @impact_story = current_partner.impact_stories.find(params[:id]) - end + def show + @impact_story = current_partner.impact_stories.find(params[:id]) + end - def new - @impact_story = current_partner.impact_stories.new - end + def new + @impact_story = current_partner.impact_stories.new + end - def create - @impact_story = current_partner.impact_stories.new(impact_story_params) + 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 + 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 edit + @impact_story = current_partner.impact_stories.find(params[:id]) + end - def update - @impact_story = current_partner.impact_stories.find(params[:id]) + 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 + if @impact_story.update(impact_story_params) + redirect_to @impact_story, notice: "Impact Story was successfully updated." + else + render :edit end + end - def email - ImpactStoryMailer.impact_story_email(current_partner.impact_stories.find(params[:id])).deliver_now - end - - private + private - def impact_story_params - params.require(:impact_story).permit(:title, :content) - end + def impact_story_params + params.require(:impact_story).permit(:title, :content) + end end diff --git a/app/models/impact_story.rb b/app/models/impact_story.rb index 70a7d3e3..6f67dee3 100644 --- a/app/models/impact_story.rb +++ b/app/models/impact_story.rb @@ -1,7 +1,7 @@ class ImpactStory < ApplicationRecord - belongs_to :partner + belongs_to :partner - def blurb(limit) - if content.length > limit then "#{content[0, limit]}…" else content end - end + def blurb(limit) + if content.length > limit then "#{content[0, limit]}…" else content end + end end diff --git a/app/models/partner.rb b/app/models/partner.rb index 9d0bd9b8..15ca7fe2 100644 --- a/app/models/partner.rb +++ b/app/models/partner.rb @@ -99,7 +99,7 @@ class Partner < ApplicationRecord has_many :children, through: :families has_many :authorized_family_members, through: :families - has_many :impact_stories + has_many :impact_stories, dependent: :destroy has_many :partner_requests, dependent: :destroy has_many :family_requests, dependent: :destroy diff --git a/spec/features/impact_story_feature_spec.rb b/spec/features/impact_story_feature_spec.rb index e3aac358..c11a0ad0 100644 --- a/spec/features/impact_story_feature_spec.rb +++ b/spec/features/impact_story_feature_spec.rb @@ -1,35 +1,35 @@ require "rails_helper" describe ImpactStory, type: :feature, include_shared: true, js: true do - let(:partner) { create(:partner, :verified, id: 3) } - let(:user) { create(:user, partner: partner) } + let(:partner) { create(:partner, :verified, id: 3) } + let(:user) { create(:user, partner: partner) } - before do - Flipper[:impact_story_requests].enable(partner) - sign_in(user) - visit(root_path) - end + before do + Flipper[:impact_story_requests].enable(partner) + sign_in(user) + visit(root_path) + end - scenario "User can see a list of impact stories" do - diaper_type = "Magic diaper" - stub_request(:any, "#{ENV["DIAPERBANK_ENDPOINT"]}/partner_requests/#{partner.id}") - .to_return(body: [{ id: 1, name: diaper_type }].to_json, status: 200) + scenario "User can see a list of impact stories" do + diaper_type = "Magic diaper" + stub_request(:any, "#{ENV["DIAPERBANK_ENDPOINT"]}/partner_requests/#{partner.id}") + .to_return(body: [{ id: 1, name: diaper_type }].to_json, status: 200) - impact_stories = [ - create(:impact_story, title: "Title1", content: "Content 1", partner: partner), - create(:impact_story, title: "Title2", content: "Content 2", partner: partner) - ].reverse + impact_stories = [ + create(:impact_story, title: "Title1", content: "Content 1", partner: partner), + create(:impact_story, title: "Title2", content: "Content 2", partner: partner) + ].reverse - click_link "Share Our Impact" - impact_stories.each.with_index do |story, index| - within "tbody" do - expect(find("tr:nth-child(#{index + 1}) td:nth-child(1)")) - .to have_text(story.title) - expect(find("tr:nth-child(#{index + 1}) td:nth-child(2)")) - .to have_text(story.created_at.strftime("%B %-d %Y")) - expect(find("tr:nth-child(#{index + 1}) td:nth-child(3)")) - .to have_text(story.content) - end - end + click_link "Share Our Impact" + impact_stories.each.with_index do |story, index| + within "tbody" do + expect(find("tr:nth-child(#{index + 1}) td:nth-child(1)")) + .to have_text(story.title) + expect(find("tr:nth-child(#{index + 1}) td:nth-child(2)")) + .to have_text(story.created_at.strftime("%B %-d %Y")) + expect(find("tr:nth-child(#{index + 1}) td:nth-child(3)")) + .to have_text(story.content) + end end + end end diff --git a/spec/models/impact_story_spec.rb b/spec/models/impact_story_spec.rb index 9ac260e2..3db66271 100644 --- a/spec/models/impact_story_spec.rb +++ b/spec/models/impact_story_spec.rb @@ -2,7 +2,7 @@ RSpec.describe ImpactStory, type: :model do it "returns the first part of the content according to the given limit" do - impact_story = + impact_story = create(:impact_story, title: "For Testing", content: "Making some long content that needs to be shortened according to a given limit") expect("Making some long con…").to eq(impact_story.blurb(20)) end diff --git a/spec/requests/impact_stories_request_spec.rb b/spec/requests/impact_stories_request_spec.rb index 58b47fea..ba722f47 100644 --- a/spec/requests/impact_stories_request_spec.rb +++ b/spec/requests/impact_stories_request_spec.rb @@ -1,48 +1,48 @@ require 'rails_helper' RSpec.describe "ImpactStories", type: :request do - let(:partner) { create(:partner) } - let!(:user) { create(:user, partner: partner) } + let(:partner) { create(:partner) } + let!(:user) { create(:user, partner: partner) } - before do - sign_in user - end + before do + sign_in user + end - describe "GET #index" do - it "return http sucess" do - get impact_stories_path + describe "GET #index" do + it "return http sucess" do + get impact_stories_path - expect(response).to have_http_status(:ok) - end + expect(response).to have_http_status(:ok) end + end - describe "GET #new" do - it "should return status code 200" do - get new_impact_story_path + describe "GET #new" do + it "should return status code 200" do + get new_impact_story_path - expect(response).to have_http_status :ok - end + expect(response).to have_http_status :ok end + end - describe "POST #create" do - it "should create and redirect to impact_story_path" do - post impact_stories_path, params: { impact_story: attributes_for(:impact_story) } + describe "POST #create" do + it "should create and redirect to impact_story_path" do + post impact_stories_path, params: { impact_story: attributes_for(:impact_story) } - impact_story = ImpactStory.select(:id).last + impact_story = ImpactStory.select(:id).last - expect(response).to redirect_to(impact_story_path(impact_story.id)) - expect(request.flash[:notice]).to eql "Impact Story was successfully created." - end + expect(response).to redirect_to(impact_story_path(impact_story.id)) + expect(request.flash[:notice]).to eql "Impact Story was successfully created." end + end - describe "PUT #update" do - let(:impact_story) { create(:impact_story, partner: partner) } + describe "PUT #update" do + let(:impact_story) { create(:impact_story, partner: partner) } - it "should update and redirect to impact_story_path" do - put impact_story_path(impact_story), params: { impact_story: attributes_for(:impact_story) } + it "should update and redirect to impact_story_path" do + put impact_story_path(impact_story), params: { impact_story: attributes_for(:impact_story) } - expect(response).to redirect_to(impact_story_path(impact_story.id)) - expect(request.flash[:notice]).to eql "Impact Story was successfully updated." - end + expect(response).to redirect_to(impact_story_path(impact_story.id)) + expect(request.flash[:notice]).to eql "Impact Story was successfully updated." end + end end From a5a322572a0e09d8e5427ee2edb2b5390cea33ef Mon Sep 17 00:00:00 2001 From: fosterv2 Date: Sun, 13 Sep 2020 08:34:50 -0700 Subject: [PATCH 07/11] Add schema information to impact_story.rb and change enumerator in contoller to use a block --- app/controllers/impact_stories_controller.rb | 4 +++- app/models/impact_story.rb | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/controllers/impact_stories_controller.rb b/app/controllers/impact_stories_controller.rb index 8034c4b1..acfaf235 100644 --- a/app/controllers/impact_stories_controller.rb +++ b/app/controllers/impact_stories_controller.rb @@ -2,7 +2,9 @@ class ImpactStoriesController < ApplicationController before_action :authenticate_user! def index - @impact_stories = current_partner.impact_stories.sort_by { |story| story.created_at }.reverse + @impact_stories = current_partner.impact_stories.sort_by do |story| + story.created_at + end.reverse end def show diff --git a/app/models/impact_story.rb b/app/models/impact_story.rb index 6f67dee3..e314c9dd 100644 --- a/app/models/impact_story.rb +++ b/app/models/impact_story.rb @@ -1,3 +1,15 @@ +# == Schema Information +# +# Table name: impact_stories +# +# id :bigint(8) not null, primary key +# partner_id :integer +# title :string +# content :text +# created_at :datetime not null +# updated_at :datetime not null +# + class ImpactStory < ApplicationRecord belongs_to :partner From feff48d63738bd72bdd4ea7e20d2155ead2ac467 Mon Sep 17 00:00:00 2001 From: fosterv2 Date: Sun, 13 Sep 2020 08:54:56 -0700 Subject: [PATCH 08/11] Use rubocop to fix layout and style --- app/controllers/impact_stories_controller.rb | 8 +++----- spec/requests/impact_stories_request_spec.rb | 18 +++++++++--------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/app/controllers/impact_stories_controller.rb b/app/controllers/impact_stories_controller.rb index acfaf235..14d2f49b 100644 --- a/app/controllers/impact_stories_controller.rb +++ b/app/controllers/impact_stories_controller.rb @@ -2,9 +2,7 @@ class ImpactStoriesController < ApplicationController before_action :authenticate_user! def index - @impact_stories = current_partner.impact_stories.sort_by do |story| - story.created_at - end.reverse + @impact_stories = current_partner.impact_stories.sort_by(&:created_at).reverse end def show @@ -17,7 +15,7 @@ def new 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 @@ -31,7 +29,7 @@ def edit 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 diff --git a/spec/requests/impact_stories_request_spec.rb b/spec/requests/impact_stories_request_spec.rb index ba722f47..2a79a7c2 100644 --- a/spec/requests/impact_stories_request_spec.rb +++ b/spec/requests/impact_stories_request_spec.rb @@ -11,36 +11,36 @@ describe "GET #index" do it "return http sucess" do get impact_stories_path - + expect(response).to have_http_status(:ok) end end - + describe "GET #new" do it "should return status code 200" do get new_impact_story_path - + expect(response).to have_http_status :ok end end - + describe "POST #create" do it "should create and redirect to impact_story_path" do post impact_stories_path, params: { impact_story: attributes_for(:impact_story) } - + impact_story = ImpactStory.select(:id).last - + expect(response).to redirect_to(impact_story_path(impact_story.id)) expect(request.flash[:notice]).to eql "Impact Story was successfully created." end end - + describe "PUT #update" do let(:impact_story) { create(:impact_story, partner: partner) } - + it "should update and redirect to impact_story_path" do put impact_story_path(impact_story), params: { impact_story: attributes_for(:impact_story) } - + expect(response).to redirect_to(impact_story_path(impact_story.id)) expect(request.flash[:notice]).to eql "Impact Story was successfully updated." end From 4d785ebf652d9dfec137e33ea2dcae135a63cd21 Mon Sep 17 00:00:00 2001 From: fosterv2 Date: Sun, 13 Sep 2020 09:09:18 -0700 Subject: [PATCH 09/11] Fix rubocop offence --- spec/features/impact_story_feature_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/features/impact_story_feature_spec.rb b/spec/features/impact_story_feature_spec.rb index c11a0ad0..a70fec43 100644 --- a/spec/features/impact_story_feature_spec.rb +++ b/spec/features/impact_story_feature_spec.rb @@ -14,7 +14,7 @@ diaper_type = "Magic diaper" stub_request(:any, "#{ENV["DIAPERBANK_ENDPOINT"]}/partner_requests/#{partner.id}") .to_return(body: [{ id: 1, name: diaper_type }].to_json, status: 200) - + impact_stories = [ create(:impact_story, title: "Title1", content: "Content 1", partner: partner), create(:impact_story, title: "Title2", content: "Content 2", partner: partner) From 7e30f6d293ef589e3ed8c8d9d2ab3a30f3b45c2a Mon Sep 17 00:00:00 2001 From: fosterv2 Date: Mon, 14 Sep 2020 14:07:13 -0700 Subject: [PATCH 10/11] Delete unnecessary files, add constraints and validations, and add more tests to ImpactStory model --- app/assets/stylesheets/impact_stories.scss | 3 --- app/helpers/impact_stories_helper.rb | 2 -- app/models/impact_story.rb | 10 ++++++---- app/views/impact_stories/_list.html.erb | 3 +-- config/routes.rb | 2 +- .../20200905195722_create_impact_stories.rb | 6 +++--- db/schema.rb | 8 +++++--- spec/factories/impact_stories.rb | 16 +++++++++++++-- spec/helpers/impact_stories_helper_spec.rb | 15 -------------- spec/models/impact_story_spec.rb | 20 +++++++++++++++---- 10 files changed, 46 insertions(+), 39 deletions(-) delete mode 100644 app/assets/stylesheets/impact_stories.scss delete mode 100644 app/helpers/impact_stories_helper.rb delete mode 100644 spec/helpers/impact_stories_helper_spec.rb diff --git a/app/assets/stylesheets/impact_stories.scss b/app/assets/stylesheets/impact_stories.scss deleted file mode 100644 index 27752b40..00000000 --- a/app/assets/stylesheets/impact_stories.scss +++ /dev/null @@ -1,3 +0,0 @@ -// Place all the styles related to the ImpactStories controller here. -// They will automatically be included in application.css. -// You can use Sass (SCSS) here: https://sass-lang.com/ diff --git a/app/helpers/impact_stories_helper.rb b/app/helpers/impact_stories_helper.rb deleted file mode 100644 index 5c164c70..00000000 --- a/app/helpers/impact_stories_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module ImpactStoriesHelper -end diff --git a/app/models/impact_story.rb b/app/models/impact_story.rb index e314c9dd..bb3e22c6 100644 --- a/app/models/impact_story.rb +++ b/app/models/impact_story.rb @@ -3,9 +3,9 @@ # Table name: impact_stories # # id :bigint(8) not null, primary key -# partner_id :integer -# title :string -# content :text +# partner_id :bigint(8) +# title :string not null +# content :text not null # created_at :datetime not null # updated_at :datetime not null # @@ -13,7 +13,9 @@ class ImpactStory < ApplicationRecord belongs_to :partner + validates :title, :content, presence: true + def blurb(limit) - if content.length > limit then "#{content[0, limit]}…" else content end + content.truncate(limit, separator: ' ') end end diff --git a/app/views/impact_stories/_list.html.erb b/app/views/impact_stories/_list.html.erb index 04d6e494..f8d2e2d9 100644 --- a/app/views/impact_stories/_list.html.erb +++ b/app/views/impact_stories/_list.html.erb @@ -5,7 +5,6 @@ Date Created Content - <%# link_to 'Export Results To CSV', families_path(nil, format: :csv), class: "btn btn-info pull-right" %> @@ -14,7 +13,7 @@ <%= story.title %> <%= story.created_at.strftime("%B %-d %Y") %> - <%= story.blurb(30) %> + <%= story.blurb(40) %> <%= link_to 'View Story', story, class: "btn btn-sm btn-info pull-right" %> <%= link_to 'Edit Story Details', edit_impact_story_path(story), class: "btn btn-sm btn-success pull-right" %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index c6375cdf..6f3bd4cd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -66,7 +66,7 @@ end end - resources :impact_stories + 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" diff --git a/db/migrate/20200905195722_create_impact_stories.rb b/db/migrate/20200905195722_create_impact_stories.rb index 99830caa..0d590011 100644 --- a/db/migrate/20200905195722_create_impact_stories.rb +++ b/db/migrate/20200905195722_create_impact_stories.rb @@ -1,9 +1,9 @@ class CreateImpactStories < ActiveRecord::Migration[6.0] def change create_table :impact_stories do |t| - t.string :title - t.text :content - t.integer :partner_id + t.string :title, null: false + t.text :content, null: false + t.references :partner, index: true, foreign_key: true t.timestamps end diff --git a/db/schema.rb b/db/schema.rb index c499b683..8b4ac860 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -120,11 +120,12 @@ end create_table "impact_stories", force: :cascade do |t| - t.string "title" - t.text "content" - t.integer "partner_id" + 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| @@ -281,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 diff --git a/spec/factories/impact_stories.rb b/spec/factories/impact_stories.rb index a80ea781..72fdd39f 100644 --- a/spec/factories/impact_stories.rb +++ b/spec/factories/impact_stories.rb @@ -1,7 +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 { "MyTitle" } - content { "MyText" } + title { Faker::Lorem.word } + content { Faker::Lorem.paragraph } partner end end diff --git a/spec/helpers/impact_stories_helper_spec.rb b/spec/helpers/impact_stories_helper_spec.rb deleted file mode 100644 index 1ba35187..00000000 --- a/spec/helpers/impact_stories_helper_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'rails_helper' - -# Specs in this file have access to a helper object that includes -# the ImpactStoriesHelper. For example: -# -# describe ImpactStoriesHelper do -# describe "string concat" do -# it "concats two strings with spaces" do -# expect(helper.concat_strings("this","that")).to eq("this that") -# end -# end -# end -RSpec.describe ImpactStoriesHelper, type: :helper do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/models/impact_story_spec.rb b/spec/models/impact_story_spec.rb index 3db66271..969c3c28 100644 --- a/spec/models/impact_story_spec.rb +++ b/spec/models/impact_story_spec.rb @@ -1,9 +1,21 @@ require 'rails_helper' RSpec.describe ImpactStory, type: :model do - it "returns the first part of the content according to the given limit" do - impact_story = - create(:impact_story, title: "For Testing", content: "Making some long content that needs to be shortened according to a given limit") - expect("Making some long con…").to eq(impact_story.blurb(20)) + describe 'associations' do + it { should belong_to(:partner).class_name('Partner') } + end + + describe 'validations' do + it { should validate_presence_of(:title) } + it { should validate_presence_of(:content) } + end + + describe '#blurb' do + subject { impact_story.blurb(10) } + let(:impact_story) { create(:impact_story, title: Faker::Lorem.word, content: Faker::Lorem.paragraph) } + + it "returns truncates the content to less than or equal to 10 characters" do + expect(subject.length).to be <= 20 + end end end From d58631fcdd14b38f730f476be013bc8337e0ee98 Mon Sep 17 00:00:00 2001 From: fosterv2 Date: Mon, 14 Sep 2020 14:31:47 -0700 Subject: [PATCH 11/11] Fix rubocop offense --- spec/models/impact_story_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/models/impact_story_spec.rb b/spec/models/impact_story_spec.rb index 969c3c28..abba4129 100644 --- a/spec/models/impact_story_spec.rb +++ b/spec/models/impact_story_spec.rb @@ -9,11 +9,11 @@ it { should validate_presence_of(:title) } it { should validate_presence_of(:content) } end - + describe '#blurb' do subject { impact_story.blurb(10) } let(:impact_story) { create(:impact_story, title: Faker::Lorem.word, content: Faker::Lorem.paragraph) } - + it "returns truncates the content to less than or equal to 10 characters" do expect(subject.length).to be <= 20 end