-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #441 from alphagov/promote-feedback-to-ar-model
Promote feedback model to a full AR model for persistence
- Loading branch information
Showing
15 changed files
with
133 additions
and
82 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
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 @@ | ||
class FeedbackEmailJob < EmailJob | ||
self.mailer = FeedbackMailer | ||
self.email = :send_feedback | ||
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 |
---|---|---|
@@ -1,20 +1,5 @@ | ||
class Feedback | ||
include ActiveModel::Validations | ||
include ActiveModel::Conversion | ||
|
||
validates_presence_of :comment | ||
validates_format_of :email, with: EMAIL_REGEX, allow_blank: true | ||
|
||
attr_accessor :email, :petition_link_or_title, :comment, :user_agent | ||
|
||
def initialize(options = {}) | ||
@email = options[:email] | ||
@petition_link_or_title = options[:petition_link_or_title] | ||
@comment = options[:comment] | ||
@user_agent = options[:user_agent] | ||
end | ||
|
||
def persisted? | ||
false | ||
end | ||
class Feedback < ActiveRecord::Base | ||
validates :comment, presence: true, length: { maximum: 32768 } | ||
validates :petition_link_or_title, length: { maximum: 255 }, allow_blank: true | ||
validates :email, format: { with: EMAIL_REGEX }, length: { maximum: 255 }, allow_blank: true | ||
end |
File renamed without changes.
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
Comment: <%= @feedback.comment %> | ||
Comments: <%= @feedback.comment %> | ||
Link: <%= @feedback.petition_link_or_title %> | ||
Email: <%= @feedback.email %> | ||
Browser: <%= @feedback.user_agent %> |
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 |
---|---|---|
@@ -1,16 +1,5 @@ | ||
# Be sure to restart your server when you modify this file. | ||
|
||
# Add new inflection rules using the following format. Inflections | ||
# are locale specific, and you may define rules for as many different | ||
# locales as you wish. All of these examples are active by default: | ||
# ActiveSupport::Inflector.inflections(:en) do |inflect| | ||
# inflect.plural /^(ox)$/i, '\1en' | ||
# inflect.singular /^(ox)en/i, '\1' | ||
# inflect.irregular 'person', 'people' | ||
# inflect.uncountable %w( fish sheep ) | ||
# end | ||
|
||
# These inflection rules are supported but not enabled by default: | ||
# ActiveSupport::Inflector.inflections(:en) do |inflect| | ||
# inflect.acronym 'RESTful' | ||
# end | ||
ActiveSupport::Inflector.inflections(:en) do |inflect| | ||
inflect.uncountable %w( feedback ) | ||
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
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,10 @@ | ||
class CreateFeedback < ActiveRecord::Migration | ||
def change | ||
create_table :feedback do |t| | ||
t.string :comment, limit: 32768, null: false | ||
t.string :petition_link_or_title | ||
t.string :email | ||
t.string :user_agent | ||
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 |
---|---|---|
@@ -1,12 +1,11 @@ | ||
Then(/^I should be able to submit feedback$/) do | ||
page.driver.browser.header('User-Agent', 'Chrome') | ||
|
||
@feedback = Feedback.new(:name => "Joe Public", :email => "[email protected]", | ||
:comment => "I can't submit a petition for some reason", :petition_link_or_title => 'link') | ||
@feedback = FactoryGirl.create(:feedback, comment: "I can't submit a petition for some reason") | ||
|
||
fill_in "feedback[email]", :with => @feedback.email | ||
fill_in "feedback[petition_link_or_title]", :with => @feedback.petition_link_or_title | ||
fill_in "feedback[comment]", :with => @feedback.comment | ||
fill_in "feedback[email]", with: @feedback.email | ||
fill_in "feedback[petition_link_or_title]", with: @feedback.petition_link_or_title | ||
fill_in "feedback[comment]", with: @feedback.comment | ||
|
||
click_button("Send feedback") | ||
expect(page).to have_content("Thank") | ||
|
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 |
---|---|---|
|
@@ -339,4 +339,11 @@ | |
end_date { 2.years.ago } | ||
end | ||
end | ||
|
||
factory :feedback do | ||
comment "This thing is wrong" | ||
petition_link_or_title "Do stuff" | ||
email "[email protected]" | ||
user_agent "Mozilla/5.0" | ||
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 |
---|---|---|
@@ -1,40 +1,28 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Feedback, type: :model do | ||
it "can be constructed without params" do | ||
expect(Feedback.new.comment).to be_nil | ||
it "has a valid factory" do | ||
expect(FactoryGirl.build(:feedback)).to be_valid | ||
end | ||
|
||
it "has an email" do | ||
expect(Feedback.new(:email => 'foo').email).to eq('foo') | ||
describe "schema" do | ||
it { is_expected.to have_db_column(:comment).of_type(:string).with_options(limit: 32768, null: false) } | ||
it { is_expected.to have_db_column(:petition_link_or_title).of_type(:string) } | ||
it { is_expected.to have_db_column(:email).of_type(:string) } | ||
it { is_expected.to have_db_column(:user_agent).of_type(:string) } | ||
end | ||
|
||
it "has a petition link or title" do | ||
expect(Feedback.new(:petition_link_or_title => 'foo').petition_link_or_title).to eq('foo') | ||
end | ||
|
||
it "has a comment" do | ||
expect(Feedback.new(:comment => 'foo').comment).to eq('foo') | ||
end | ||
|
||
def valid_attributes | ||
{ :email => "[email protected]", | ||
:comment => "I can't submit a petition for some reason", | ||
:petition_link_or_title => 'link' } | ||
end | ||
|
||
describe "valid?" do | ||
it "is valid when all attributes are in place" do | ||
expect(Feedback.new(valid_attributes)).to be_valid | ||
end | ||
|
||
it "is not valid when a required attribute is missing" do | ||
expect(Feedback.new(valid_attributes.except(:comment))).not_to be_valid | ||
describe "validations" do | ||
it "is invalid by default" do | ||
expect(subject).not_to be_valid | ||
end | ||
|
||
it "is not valid when the email format is wrong" do | ||
expect(Feedback.new(valid_attributes.merge(:email => 'foo'))).not_to be_valid | ||
end | ||
it { is_expected.to validate_presence_of(:comment) } | ||
it { is_expected.to validate_length_of(:comment).is_at_most(32768) } | ||
it { is_expected.to validate_length_of(:petition_link_or_title).is_at_most(255) } | ||
it { is_expected.to validate_length_of(:email).is_at_most(255) } | ||
it { is_expected.to allow_value("[email protected]").for(:email) } | ||
it { is_expected.not_to allow_value("foo@example").for(:email) } | ||
it { is_expected.not_to allow_value("foo").for(:email) } | ||
end | ||
|
||
end |