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] 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 %> +