From fa6f41b512b2586f7573ab9796511ea1fcf8f670 Mon Sep 17 00:00:00 2001 From: deppbot Date: Wed, 12 Jul 2017 08:34:33 +0800 Subject: [PATCH 01/54] Bundle Update on 2017-07-12 --- Gemfile.lock | 79 +++++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 38 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index f2a314780..3ad07dcfd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -52,7 +52,7 @@ GEM safely_block (>= 0.1.1) user_agent_parser uuidtools - airbrussh (1.2.0) + airbrussh (1.3.0) sshkit (>= 1.6.1, != 1.7.0) akami (1.3.1) gyoku (>= 0.4.0) @@ -73,7 +73,7 @@ GEM uniform_notifier (~> 1.10.0) byebug (9.0.6) cancancan (1.16.0) - capistrano (3.8.1) + capistrano (3.8.2) airbrussh (>= 1.0.0) i18n rake (>= 10.0.0) @@ -87,7 +87,7 @@ GEM capistrano3-delayed-job (1.7.3) capistrano (~> 3.0, >= 3.0.0) daemons (~> 1.2.4) - capybara (2.14.0) + capybara (2.14.4) addressable mime-types (>= 1.16) nokogiri (>= 1.3.3) @@ -95,17 +95,17 @@ GEM rack-test (>= 0.5.4) xpath (~> 2.0) chronic (0.10.2) - ckeditor (4.2.3) + ckeditor (4.2.4) cocaine orm_adapter (~> 0.5.0) - climate_control (0.1.0) + climate_control (0.2.0) cliver (0.3.2) cocaine (0.5.8) climate_control (>= 0.0.3, < 1.0) - cocoon (1.2.9) - coffee-rails (4.2.1) + cocoon (1.2.10) + coffee-rails (4.2.2) coffee-script (>= 2.2.0) - railties (>= 4.0.0, < 5.2.x) + railties (>= 4.0.0) coffee-script (2.4.1) coffee-script-source execjs @@ -120,11 +120,11 @@ GEM daemons (1.2.4) dalli (2.7.6) database_cleaner (1.5.3) - debug_inspector (0.0.2) - delayed_job (4.1.2) - activesupport (>= 3.0, < 5.1) - delayed_job_active_record (4.1.1) - activerecord (>= 3.0, < 5.1) + debug_inspector (0.0.3) + delayed_job (4.1.3) + activesupport (>= 3.0, < 5.2) + delayed_job_active_record (4.1.2) + activerecord (>= 3.0, < 5.2) delayed_job (>= 3.0, < 5) devise (3.5.10) bcrypt (~> 3.0) @@ -144,10 +144,10 @@ GEM json thread thread_safe - email_spec (2.1.0) + email_spec (2.1.1) htmlentities (~> 4.3.3) launchy (~> 2.1) - mail (~> 2.6.3) + mail (~> 2.6) errbase (0.0.3) erubis (2.7.0) execjs (2.7.0) @@ -158,7 +158,7 @@ GEM railties (>= 3.0.0) faker (1.7.3) i18n (~> 0.5) - faraday (0.11.0) + faraday (0.12.1) multipart-post (>= 1.2, < 3) foundation-rails (6.2.4.0) railties (>= 3.1.0) @@ -170,12 +170,12 @@ GEM activesupport (>= 4.1) railties (>= 4.1) tzinfo (~> 1.2, >= 1.2.2) - geocoder (1.4.3) + geocoder (1.4.4) globalid (0.4.0) activesupport (>= 4.2.0) - graphiql-rails (1.4.1) + graphiql-rails (1.4.2) rails - graphql (1.6.3) + graphql (1.6.4) groupdate (3.2.0) activesupport (>= 3) gyoku (1.3.1) @@ -183,9 +183,10 @@ GEM hashie (3.5.5) highline (1.7.8) htmlentities (4.3.4) - httpi (2.4.1) + httpi (2.4.2) rack - i18n (0.8.4) + socksify + i18n (0.8.6) i18n-tasks (0.9.15) activesupport (>= 4.0.2) ast (>= 2.1.0) @@ -224,7 +225,7 @@ GEM knapsack (1.13.3) rake timecop (>= 0.1.0) - kramdown (1.13.2) + kramdown (1.14.0) launchy (2.4.3) addressable (~> 2.3) letter_opener (1.4.1) @@ -259,9 +260,9 @@ GEM nokogiri (1.8.0) mini_portile2 (~> 2.2.0) nori (2.6.0) - oauth (0.5.1) - oauth2 (1.3.1) - faraday (>= 0.8, < 0.12) + oauth (0.5.3) + oauth2 (1.4.0) + faraday (>= 0.8, < 0.13) jwt (~> 1.0) multi_json (~> 1.3) multi_xml (~> 0.5) @@ -328,7 +329,7 @@ GEM bundler (>= 1.3.0, < 2.0) railties (= 4.2.9) sprockets-rails - rails-assets-markdown-it (8.2.1) + rails-assets-markdown-it (8.2.2) rails-deprecated_sanitizer (1.0.3) activesupport (>= 4.2.0.alpha) rails-dom-testing (1.0.8) @@ -386,7 +387,7 @@ GEM sshkit (>= 1.2) safely_block (0.2.0) errbase - sass (3.4.23) + sass (3.4.25) sass-rails (5.0.6) railties (>= 4.0.0, < 6) sass (~> 3.1) @@ -408,12 +409,13 @@ GEM docile (~> 1.1.0) json (>= 1.8, < 3) simplecov-html (~> 0.10.0) - simplecov-html (0.10.0) + simplecov-html (0.10.1) sitemap_generator (5.3.1) builder (~> 3.0) social-share-button (0.10.0) coffee-rails - spring (2.0.1) + socksify (1.7.1) + spring (2.0.2) activesupport (>= 4.2) spring-commands-rspec (1.0.4) spring (>= 0.9.1) @@ -428,19 +430,19 @@ GEM actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) - sshkit (1.13.1) + sshkit (1.14.0) net-scp (>= 1.1.2) net-ssh (>= 2.8.0) term-ansicolor (1.6.0) tins (~> 1.0) - terminal-table (1.7.3) - unicode-display_width (~> 1.1.1) + terminal-table (1.8.0) + unicode-display_width (~> 1.1, >= 1.1.1) thor (0.19.4) thread (0.2.2) thread_safe (0.3.6) tilt (2.0.7) - timecop (0.8.1) - tins (1.13.2) + timecop (0.9.1) + tins (1.15.0) turbolinks (2.5.3) coffee-rails turnout (2.4.0) @@ -452,14 +454,14 @@ GEM thread_safe (~> 0.1) uglifier (3.2.0) execjs (>= 0.3.0, < 3) - unicode-display_width (1.1.3) + unicode-display_width (1.3.0) unicorn (5.3.0) kgio (~> 2.6) raindrops (~> 0.7) uniform_notifier (1.10.0) - user_agent_parser (2.3.0) + user_agent_parser (2.3.1) uuidtools (2.1.5) - warden (1.2.6) + warden (1.2.7) rack (>= 1.0) wasabi (3.5.0) httpi (~> 2.0) @@ -473,7 +475,7 @@ GEM websocket-extensions (0.1.2) whenever (0.9.7) chronic (>= 0.6.3) - xpath (2.0.0) + xpath (2.1.0) nokogiri (~> 1.3) PLATFORMS @@ -558,5 +560,6 @@ DEPENDENCIES web-console (~> 3.3.0) whenever (~> 0.9.7) + BUNDLED WITH 1.15.1 From a141c82e332ade27775b58fc0ef69d67672a29ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Thu, 20 Jul 2017 12:08:42 +0200 Subject: [PATCH 02/54] Create new polymorphic model Document. --- app/models/concerns/documentable.rb | 8 ++++++ app/models/document.rb | 9 +++++++ db/migrate/20170720092638_create_documents.rb | 14 ++++++++++ db/schema.rb | 22 +++++++++++++-- spec/factories.rb | 12 +++++++++ spec/models/document_spec.rb | 8 ++++++ spec/shared/models/document_validatable.rb | 27 +++++++++++++++++++ 7 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 app/models/concerns/documentable.rb create mode 100644 app/models/document.rb create mode 100644 db/migrate/20170720092638_create_documents.rb create mode 100644 spec/models/document_spec.rb create mode 100644 spec/shared/models/document_validatable.rb diff --git a/app/models/concerns/documentable.rb b/app/models/concerns/documentable.rb new file mode 100644 index 000000000..82c6c0b6e --- /dev/null +++ b/app/models/concerns/documentable.rb @@ -0,0 +1,8 @@ +module Documentable + extend ActiveSupport::Concern + + included do + has_many :documents, as: :documentable, dependent: :destroy + end + +end diff --git a/app/models/document.rb b/app/models/document.rb new file mode 100644 index 000000000..e8dd92b56 --- /dev/null +++ b/app/models/document.rb @@ -0,0 +1,9 @@ +class Document < ActiveRecord::Base + belongs_to :user + belongs_to :documentable, polymorphic: true + + validates :user_id, presence: true + validates :documentable_id, presence: true + validates :documentable_type, presence: true + +end diff --git a/db/migrate/20170720092638_create_documents.rb b/db/migrate/20170720092638_create_documents.rb new file mode 100644 index 000000000..b7d1e287c --- /dev/null +++ b/db/migrate/20170720092638_create_documents.rb @@ -0,0 +1,14 @@ +class CreateDocuments < ActiveRecord::Migration + def change + create_table :documents do |t| + t.string :title + t.attachment :attachment + t.references :user, index: true, foreign_key: true + t.references :documentable, polymorphic: true, index: true + + t.timestamps null: false + end + + add_index :documents, [:user_id, :documentable_type, :documentable_id], name: "access_documents" + end +end diff --git a/db/schema.rb b/db/schema.rb index 91a26adeb..cccf10b9e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170719174326) do +ActiveRecord::Schema.define(version: 20170720092638) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -298,6 +298,23 @@ ActiveRecord::Schema.define(version: 20170719174326) do t.datetime "updated_at", null: false end + create_table "documents", force: :cascade do |t| + t.string "title" + t.string "attachment_file_name" + t.string "attachment_content_type" + t.integer "attachment_file_size" + t.datetime "attachment_updated_at" + t.integer "user_id" + t.integer "documentable_id" + t.string "documentable_type" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "documents", ["documentable_type", "documentable_id"], name: "index_documents_on_documentable_type_and_documentable_id", using: :btree + add_index "documents", ["user_id", "documentable_type", "documentable_id"], name: "access_documents", using: :btree + add_index "documents", ["user_id"], name: "index_documents_on_user_id", using: :btree + create_table "failed_census_calls", force: :cascade do |t| t.integer "user_id" t.string "document_number" @@ -919,7 +936,7 @@ ActiveRecord::Schema.define(version: 20170719174326) do t.boolean "email_digest", default: true t.boolean "email_on_direct_message", default: true t.boolean "official_position_badge", default: false - t.datetime "password_changed_at", default: '2017-06-22 11:21:30', null: false + t.datetime "password_changed_at", default: '2017-07-20 09:31:51', null: false t.boolean "created_from_signature", default: false t.integer "failed_email_digests_count", default: 0 t.text "former_users_data_log", default: "" @@ -1014,6 +1031,7 @@ ActiveRecord::Schema.define(version: 20170719174326) do add_foreign_key "administrators", "users" add_foreign_key "annotations", "legacy_legislations" add_foreign_key "annotations", "users" + add_foreign_key "documents", "users" add_foreign_key "failed_census_calls", "poll_officers" add_foreign_key "failed_census_calls", "users" add_foreign_key "flags", "users" diff --git a/spec/factories.rb b/spec/factories.rb index 24c0767d4..0a82fa365 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -368,6 +368,18 @@ FactoryGirl.define do end end + factory :document do + association :user, factory: :user + + trait :proposal_document do + association :documentable, factory: :proposal + end + + trait :budget_investment_document do + association :documentable, factory: :budget_investment + end + end + factory :comment do association :commentable, factory: :debate user diff --git a/spec/models/document_spec.rb b/spec/models/document_spec.rb new file mode 100644 index 000000000..bc5d4e923 --- /dev/null +++ b/spec/models/document_spec.rb @@ -0,0 +1,8 @@ +require 'rails_helper' + +describe Document do + + it_behaves_like "document validations", "budget_investment_document" + it_behaves_like "document validations", "proposal_document" + +end diff --git a/spec/shared/models/document_validatable.rb b/spec/shared/models/document_validatable.rb new file mode 100644 index 000000000..2c6cdb568 --- /dev/null +++ b/spec/shared/models/document_validatable.rb @@ -0,0 +1,27 @@ +shared_examples "document validations" do |documentable_factory| + + let(:documentable) { build(:document, documentable_factory.to_sym) } + + it "should be valid" do + expect(documentable).to be_valid + end + + it "should not be valid without a user_id" do + documentable.user_id = nil + + expect(documentable).to_not be_valid + end + + it "should not be valid without a documentable_id" do + documentable.documentable_id = nil + + expect(documentable).to_not be_valid + end + + it "should not be valid without a documentable_type" do + documentable.documentable_type = nil + + expect(documentable).to_not be_valid + end + +end From 34d06dad04d4964f25cf6da2876bec844022d3ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Fri, 21 Jul 2017 11:27:40 +0200 Subject: [PATCH 03/54] Add document model validations and model shared specs. --- app/models/document.rb | 8 +++- spec/factories.rb | 2 + spec/fixtures/files/empty.pdf | Bin 0 -> 1886 bytes spec/shared/models/document_validatable.rb | 27 ----------- spec/shared/models/document_validations.rb | 52 +++++++++++++++++++++ 5 files changed, 61 insertions(+), 28 deletions(-) create mode 100644 spec/fixtures/files/empty.pdf delete mode 100644 spec/shared/models/document_validatable.rb create mode 100644 spec/shared/models/document_validations.rb diff --git a/app/models/document.rb b/app/models/document.rb index e8dd92b56..688540c09 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -1,8 +1,14 @@ class Document < ActiveRecord::Base + has_attached_file :attachment + belongs_to :user belongs_to :documentable, polymorphic: true - validates :user_id, presence: true + validates_attachment :attachment, presence: true, + content_type: { content_type: "application/pdf" }, + size: { in: 0..3.megabytes } + validates :title, presence: true + validates :user, presence: true validates :documentable_id, presence: true validates :documentable_type, presence: true diff --git a/spec/factories.rb b/spec/factories.rb index 0a82fa365..7b1f6a417 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -369,7 +369,9 @@ FactoryGirl.define do end factory :document do + sequence(:title) { |n| "Document title #{n}" } association :user, factory: :user + attachment { File.new("spec/fixtures/files/empty.pdf") } trait :proposal_document do association :documentable, factory: :proposal diff --git a/spec/fixtures/files/empty.pdf b/spec/fixtures/files/empty.pdf new file mode 100644 index 0000000000000000000000000000000000000000..de2e381483b397922ce1810b1ca6eed5142a1b1f GIT binary patch literal 1886 zcmcIlO>f#j5cPR~#a<$%hp=87d+jR9A<(o@OG6=2E1?|L_8OcRFSVD7^weM8cE;F1 zarz-eMM6TeZ)RuSd$XL3hgWC3*QbP2o9gJEoSzdmmUC562O<-~ZXZ{QvVk-**K=}l zK~#|-5Rco0Ev8N*>}{4*bVglFr+0)6^tv#VAEe?@Hx=(K6+oMdC#urxGKNmDr%yN0 zy^0O|rtErY)1WR7?XYpFgQp(3uS}FZ5{8cd(OEubv9)!oa;1?PCR) zc(Gnalp`aPgRb$k?fRW6=4L^?7E+l~$tC$*(`ZvnwQ0O0b*n>7J`kj>K{W(oGS+iM zODRr(kr`#63uU)zV{lrKV*pGxd7?o9D|s#qrqb^DklFd`@HjmqW=R1l=hbva$hSW%7>0$G=tsm7`t=;cZm zs9pd!j%4*f`Ia5la23{g=(u6Qvc7dWK@g7Nw!_xfV1y0Tmn>GVOZm7vJM`>-8l`ET zZ$0^!?rYSq?H^cuFBWgDv>&rI16uu`^c%_N>37KIlOnd0*K37N`;%_GweEhR1Dmqj z%;bume*a-9YiX@4V?A2d3tUJgO{R;XG;ln8>DXM@!r|O@JuUz+(#dbnVDFQ44E8-` z>#AhAj%CgUD$ixfGTa&XX7K|PJ-g?f^;xxm=Y=IY-fq;WK0OnRWksSky@grkFAg?p zQ>v7}DhI^geW>qwu18b4i=jX9LyLXP0~a5~IEMv2in);0i3Lx@!ftNg1;UT&^*x~c zDCT=zoG-wXM>t`_E*-^!em6Jp!Mew|poC6ukgme zTXJ2bnsPJ~JT?=p;T_ukz8=!qxhwj@4$?Fgf`>`sg(6gOAiTu$ Date: Fri, 21 Jul 2017 11:55:15 +0200 Subject: [PATCH 04/54] Add abilities to documentable authors and admins to create and destroy documents. --- app/models/abilities/administrator.rb | 1 + app/models/abilities/common.rb | 2 ++ spec/models/abilities/administrator_spec.rb | 12 ++++++++++++ spec/models/abilities/common_spec.rb | 21 +++++++++++++++++++++ 4 files changed, 36 insertions(+) diff --git a/app/models/abilities/administrator.rb b/app/models/abilities/administrator.rb index bc2fea5d4..b8f80c0cd 100644 --- a/app/models/abilities/administrator.rb +++ b/app/models/abilities/administrator.rb @@ -73,6 +73,7 @@ module Abilities can [:manage], ::Legislation::Question cannot :comment_as_moderator, [::Legislation::Question, Legislation::Annotation] + can [:create, :destroy], Document end end end diff --git a/app/models/abilities/common.rb b/app/models/abilities/common.rb index 33a9d50f6..4eb202332 100644 --- a/app/models/abilities/common.rb +++ b/app/models/abilities/common.rb @@ -36,6 +36,8 @@ module Abilities can [:create, :destroy], Follow + can [:create, :destroy], Document, documentable: { author_id: user.id } + unless user.organization? can :vote, Debate can :vote, Comment diff --git a/spec/models/abilities/administrator_spec.rb b/spec/models/abilities/administrator_spec.rb index da872e353..bbec927a0 100644 --- a/spec/models/abilities/administrator_spec.rb +++ b/spec/models/abilities/administrator_spec.rb @@ -12,8 +12,12 @@ describe "Abilities::Administrator" do let(:debate) { create(:debate) } let(:comment) { create(:comment) } let(:proposal) { create(:proposal) } + let(:budget_investment) { create(:budget_investment) } let(:legislation_question) { create(:legislation_question) } + let(:proposal_document) { build(:document, documentable: proposal) } + let(:budget_investment_document) { build(:document, documentable: budget_investment) } + let(:hidden_debate) { create(:debate, :hidden) } let(:hidden_comment) { create(:comment, :hidden) } let(:hidden_proposal) { create(:proposal, :hidden) } @@ -71,4 +75,12 @@ describe "Abilities::Administrator" do it { should be_able_to(:valuate, create(:budget_investment, budget: create(:budget, phase: 'valuating'))) } it { should be_able_to(:valuate, create(:budget_investment, budget: create(:budget, phase: 'finished'))) } + + it { should be_able_to(:new, proposal_document) } + it { should be_able_to(:create, proposal_document) } + it { should be_able_to(:destroy, proposal_document) } + + it { should be_able_to(:new, budget_investment_document) } + it { should be_able_to(:create, budget_investment_document) } + it { should be_able_to(:destroy, budget_investment_document) } end diff --git a/spec/models/abilities/common_spec.rb b/spec/models/abilities/common_spec.rb index da398e610..a3f8227b3 100644 --- a/spec/models/abilities/common_spec.rb +++ b/spec/models/abilities/common_spec.rb @@ -54,6 +54,11 @@ describe "Abilities::Common" do let(:incoming_poll_question_from_other_geozone) { create(:poll_question, poll: incoming_poll_from_other_geozone) } let(:incoming_poll_question_from_all_geozones) { create(:poll_question, poll: incoming_poll) } + let(:own_proposal_document) { build(:document, documentable: own_proposal) } + let(:proposal_document) { build(:document, documentable: proposal) } + let(:own_budget_investment_document) { build(:document, documentable: own_investment_in_accepting_budget) } + let(:budget_investment_document) { build(:document, documentable: investment_in_accepting_budget) } + it { should be_able_to(:index, Debate) } it { should be_able_to(:show, debate) } it { should be_able_to(:vote, debate) } @@ -82,6 +87,22 @@ describe "Abilities::Common" do it { should_not be_able_to(:create, DirectMessage) } it { should_not be_able_to(:show, DirectMessage) } + it { should be_able_to(:new, own_proposal_document) } + it { should be_able_to(:create, own_proposal_document) } + it { should be_able_to(:destroy, own_proposal_document) } + + it { should_not be_able_to(:new, proposal_document) } + it { should_not be_able_to(:create, proposal_document) } + it { should_not be_able_to(:destroy, proposal_document) } + + it { should be_able_to(:new, own_budget_investment_document) } + it { should be_able_to(:create, own_budget_investment_document) } + it { should be_able_to(:destroy, own_budget_investment_document) } + + it { should_not be_able_to(:new, budget_investment_document) } + it { should_not be_able_to(:create, budget_investment_document) } + it { should_not be_able_to(:destroy, budget_investment_document) } + describe 'flagging content' do it { should be_able_to(:flag, debate) } it { should be_able_to(:unflag, debate) } From a2130689edc41d1729c7389d536e95d786915658 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Fri, 21 Jul 2017 13:46:13 +0200 Subject: [PATCH 05/54] Create documentable concern and add it to budget investment model. Create documents controller, documents helper, routes, translations and specs --- app/assets/javascripts/forms.js.coffee | 11 + app/assets/stylesheets/icons.scss | 4 + .../budgets/investments_controller.rb | 1 + app/controllers/documents_controller.rb | 47 ++++ app/helpers/documents_helper.rb | 15 + app/models/budget/investment.rb | 1 + .../investments/_filter_subnav.html.erb | 9 + .../investments/_investment_show.html.erb | 8 +- app/views/budgets/investments/show.html.erb | 5 + app/views/documents/_document.html.erb | 14 + app/views/documents/_documents.html.erb | 17 ++ app/views/documents/_form.html.erb | 38 +++ app/views/documents/new.html.erb | 20 ++ config/locales/en/activerecord.yml | 6 + config/locales/en/documents.yml | 21 ++ config/locales/en/general.yml | 1 + config/locales/es/activerecord.yml | 6 + config/locales/es/documents.yml | 21 ++ config/locales/es/general.yml | 1 + config/routes.rb | 2 + spec/features/budgets/investments_spec.rb | 2 + spec/shared/features/documentable.rb | 256 ++++++++++++++++++ 22 files changed, 505 insertions(+), 1 deletion(-) create mode 100644 app/controllers/documents_controller.rb create mode 100644 app/helpers/documents_helper.rb create mode 100644 app/views/documents/_document.html.erb create mode 100644 app/views/documents/_documents.html.erb create mode 100644 app/views/documents/_form.html.erb create mode 100644 app/views/documents/new.html.erb create mode 100644 config/locales/en/documents.yml create mode 100644 config/locales/es/documents.yml create mode 100644 spec/shared/features/documentable.rb diff --git a/app/assets/javascripts/forms.js.coffee b/app/assets/javascripts/forms.js.coffee index edf4c525f..58e0894b0 100644 --- a/app/assets/javascripts/forms.js.coffee +++ b/app/assets/javascripts/forms.js.coffee @@ -23,8 +23,19 @@ App.Forms = false ) + uploadButton: -> + element = $('input[type=file]') + i = 0 + while i < element.length + element[i].addEventListener 'change', -> + idButton = $(this) + idButton.closest('.file-name').find('p').text(@files[0].name) + return + i++ + initialize: -> App.Forms.disableEnter() App.Forms.submitOnChange() App.Forms.toggleLink() + App.Forms.uploadButton() false diff --git a/app/assets/stylesheets/icons.scss b/app/assets/stylesheets/icons.scss index 782b5cdb2..5e92ee460 100644 --- a/app/assets/stylesheets/icons.scss +++ b/app/assets/stylesheets/icons.scss @@ -97,6 +97,10 @@ content: '\72'; } +.icon-documents::before { + content: '\68'; +} + .icon-proposals::before { content: '\68'; } diff --git a/app/controllers/budgets/investments_controller.rb b/app/controllers/budgets/investments_controller.rb index 16144cb27..2170caa50 100644 --- a/app/controllers/budgets/investments_controller.rb +++ b/app/controllers/budgets/investments_controller.rb @@ -44,6 +44,7 @@ module Budgets set_comment_flags(@comment_tree.comments) load_investment_votes(@investment) @investment_ids = [@investment.id] + @document = Document.new(documentable: @investment) end def create diff --git a/app/controllers/documents_controller.rb b/app/controllers/documents_controller.rb new file mode 100644 index 000000000..51ee7f56a --- /dev/null +++ b/app/controllers/documents_controller.rb @@ -0,0 +1,47 @@ +class DocumentsController < ApplicationController + before_action :authenticate_user! + before_filter :find_documentable, except: :destroy + before_filter :prepare_new_document, only: :new + before_filter :prepare_document_for_creation, only: :create + load_and_authorize_resource + + def new + end + + def create + if @document.save + flash[:notice] = t "documents.actions.create.notice" + redirect_to params[:from] + else + flash[:alert] = t "documents.actions.create.alert" + render :new + end + end + + def destroy + @document.destroy + flash[:notice] = t "documents.actions.destroy.notice" + redirect_to params[:from] + end + + private + + def find_documentable + @documentable = params[:documentable_type].constantize.find(params[:documentable_id]) + end + + def prepare_new_document + @document = Document.new(documentable: @documentable, user_id: @documentable.author_id) + end + + def prepare_document_for_creation + @document = Document.new(document_params) + @document.documentable = @documentable + @document.user = current_user + end + + def document_params + params.require(:document).permit(:title, :documentable_type, :documentable_id, :attachment) + end + +end diff --git a/app/helpers/documents_helper.rb b/app/helpers/documents_helper.rb new file mode 100644 index 000000000..df6666ab5 --- /dev/null +++ b/app/helpers/documents_helper.rb @@ -0,0 +1,15 @@ +module DocumentsHelper + + def document_attachment_file_name(document) + document.attachment.attachment_file_name if document.attachment.exists? + end + + def errors_on_attachment(document) + document.errors[:attachment].join(', ') if document.errors.key?(:attachment) + end + + def document_documentable_class(document) + document.documentable.class.name.parameterize('_') + end + +end \ No newline at end of file diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index a5bea4508..9de59e9f3 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -7,6 +7,7 @@ class Budget include Searchable include Reclassification include Followable + include Documentable acts_as_votable acts_as_paranoid column: :hidden_at diff --git a/app/views/budgets/investments/_filter_subnav.html.erb b/app/views/budgets/investments/_filter_subnav.html.erb index a46c33b2d..8faa221ff 100644 --- a/app/views/budgets/investments/_filter_subnav.html.erb +++ b/app/views/budgets/investments/_filter_subnav.html.erb @@ -17,6 +17,15 @@ <% end %> +
  • + <%= link_to "#tab-documents" do %> +

    + <%= t("documents.tab") %> + (<%= @investment.documents.count %>) +

    + <% end %> +
  • + diff --git a/app/views/budgets/investments/_investment_show.html.erb b/app/views/budgets/investments/_investment_show.html.erb index 434934c7e..a5aa73240 100644 --- a/app/views/budgets/investments/_investment_show.html.erb +++ b/app/views/budgets/investments/_investment_show.html.erb @@ -4,6 +4,12 @@
    <%= back_link_to budget_investments_path(investment.budget, heading_id: investment.heading) %> + <% if can? :create, @document %> + <%= link_to t("documents.upload_document"), + new_document_path(documentable_id:investment, documentable_type: investment.class.name, from: request.url), + class: 'button hollow float-right' %> + <% end %> +

    <%= investment.title %>

    @@ -14,7 +20,6 @@  •  <%= investment.heading.name %>
    -

    <%= t("budgets.investments.show.code_html", code: investment.id) %> @@ -51,6 +56,7 @@

    <%= t('budgets.investments.show.price_explanation') %>

    <%= investment.price_explanation %>

    <% end %> +