From c39969210952c2dc9709ece32ef86ae0e12e5310 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Wed, 14 Jun 2017 14:43:30 +0200 Subject: [PATCH] Add image description to budget investments --- app/models/budget/investment.rb | 2 + ...image_description_to_budget_investments.rb | 5 ++ db/schema.rb | 5 +- spec/factories.rb | 5 ++ spec/models/budget/investment_spec.rb | 48 ++++++++++++++++--- 5 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 db/migrate/20170614111945_add_image_description_to_budget_investments.rb diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index 2d2e0c3c6..459317497 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -41,6 +41,8 @@ class Budget validates :description, length: { maximum: Budget::Investment.description_max_length } validates :terms_of_service, acceptance: { allow_nil: false }, on: :create validates_attachment :image, content_type: { content_type: ["image/jpeg"] } + validates :image_description, presence: true, if: -> { image.present? } + validates :image_description, length: { in: 4..Budget::Investment.title_max_length }, if: -> { image.present? } scope :sort_by_confidence_score, -> { reorder(confidence_score: :desc, id: :desc) } scope :sort_by_ballots, -> { reorder(ballot_lines_count: :desc, id: :desc) } diff --git a/db/migrate/20170614111945_add_image_description_to_budget_investments.rb b/db/migrate/20170614111945_add_image_description_to_budget_investments.rb new file mode 100644 index 000000000..baa873dd6 --- /dev/null +++ b/db/migrate/20170614111945_add_image_description_to_budget_investments.rb @@ -0,0 +1,5 @@ +class AddImageDescriptionToBudgetInvestments < ActiveRecord::Migration + def change + add_column :budget_investments, :image_description, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 0b63e2866..a44bf62aa 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -165,6 +165,7 @@ ActiveRecord::Schema.define(version: 20170918231410) do t.string "image_content_type" t.integer "image_file_size" t.datetime "image_updated_at" + t.string "image_description" end add_index "budget_investments", ["administrator_id"], name: "index_budget_investments_on_administrator_id", using: :btree @@ -256,10 +257,10 @@ ActiveRecord::Schema.define(version: 20170918231410) do t.string "visit_id" t.datetime "hidden_at" t.integer "flags_count", default: 0 - t.datetime "ignored_flag_at" t.integer "cached_votes_total", default: 0 t.integer "cached_votes_up", default: 0 t.integer "cached_votes_down", default: 0 + t.datetime "ignored_flag_at" t.integer "comments_count", default: 0 t.datetime "confirmed_hide_at" t.integer "cached_anonymous_votes_total", default: 0 @@ -278,6 +279,7 @@ ActiveRecord::Schema.define(version: 20170918231410) do add_index "debates", ["cached_votes_total"], name: "index_debates_on_cached_votes_total", using: :btree add_index "debates", ["cached_votes_up"], name: "index_debates_on_cached_votes_up", using: :btree add_index "debates", ["confidence_score"], name: "index_debates_on_confidence_score", using: :btree + add_index "debates", ["description"], name: "index_debates_on_description", using: :btree add_index "debates", ["geozone_id"], name: "index_debates_on_geozone_id", using: :btree add_index "debates", ["hidden_at"], name: "index_debates_on_hidden_at", using: :btree add_index "debates", ["hot_score"], name: "index_debates_on_hot_score", using: :btree @@ -784,6 +786,7 @@ ActiveRecord::Schema.define(version: 20170918231410) do add_index "proposals", ["cached_votes_up"], name: "index_proposals_on_cached_votes_up", using: :btree add_index "proposals", ["community_id"], name: "index_proposals_on_community_id", using: :btree add_index "proposals", ["confidence_score"], name: "index_proposals_on_confidence_score", using: :btree + add_index "proposals", ["description"], name: "index_proposals_on_description", using: :btree add_index "proposals", ["geozone_id"], name: "index_proposals_on_geozone_id", using: :btree add_index "proposals", ["hidden_at"], name: "index_proposals_on_hidden_at", using: :btree add_index "proposals", ["hot_score"], name: "index_proposals_on_hot_score", using: :btree diff --git a/spec/factories.rb b/spec/factories.rb index b90f68175..647aeec5b 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -318,6 +318,11 @@ FactoryGirl.define do feasibility "feasible" valuation_finished true end + + trait :with_descriptive_image do + image { File.new("spec/fixtures/files/logo_header.jpg") } + image_description "Lorem ipsum dolor sit amet" + end end factory :budget_ballot, class: 'Budget::Ballot' do diff --git a/spec/models/budget/investment_spec.rb b/spec/models/budget/investment_spec.rb index dda046275..2b770f573 100644 --- a/spec/models/budget/investment_spec.rb +++ b/spec/models/budget/investment_spec.rb @@ -30,27 +30,61 @@ describe Budget::Investment do end describe "#image" do + let(:investment_with_image) { build(:budget_investment, :with_descriptive_image) } - describe "extesion" do + it "should be valid" do + expect(investment_with_image).to be_valid + end + + describe "file extension" do it "should not be valid with '.png' extension" do - investment.image = File.new("spec/fixtures/files/logo_header.png") + investment_with_image.image = File.new("spec/fixtures/files/logo_header.png") - expect(investment).to_not be_valid + expect(investment_with_image).to_not be_valid end it "should not be valid with '.gif' extension" do - investment.image = File.new("spec/fixtures/files/logo_header.gif") + investment_with_image.image = File.new("spec/fixtures/files/logo_header.gif") - expect(investment).to_not be_valid + expect(investment_with_image).to_not be_valid end it "should be valid with '.jpg' extension" do - investment.image = File.new("spec/fixtures/files/logo_header.jpg") + investment_with_image.image = File.new("spec/fixtures/files/logo_header.jpg") - expect(investment).to be_valid + expect(investment_with_image).to be_valid end end + describe "description" do + + it "should be valid when image and image_description are not defined" do + investment_with_image.image = nil + investment_with_image.image_description = nil + + expect(investment_with_image).to be_valid + end + + it "should not be valid when correct image attached but no image description provided" do + investment_with_image.image_description = '' + + expect(investment_with_image).to_not be_valid + end + + it "should not be valid when image description is too short" do + investment_with_image.image_description = 'a'*3 + + expect(investment_with_image).to_not be_valid + end + + it "should not be valid when image description is too long" do + investment_with_image.image_description = 'a'*81 + + expect(investment_with_image).to_not be_valid + end + + end + end it "sanitizes description" do