Skip to content

Commit

Permalink
Merge branch 'general-fix' of https://github.com/AyuntamientoMadrid/a…
Browse files Browse the repository at this point in the history
…gendas into general-fix
  • Loading branch information
taitus committed Dec 27, 2017
2 parents 94d8916 + 7548d70 commit 603b0f5
Show file tree
Hide file tree
Showing 13 changed files with 206 additions and 25 deletions.
1 change: 1 addition & 0 deletions app/controllers/admin/organizations_controller.rb
Expand Up @@ -75,6 +75,7 @@ def organization_params
represented_entities_attributes: [:id, :identifier, :name, :first_surname, :second_surname,
:from, :fiscal_year, :range_fund, :subvention, :contract, :_destroy],
organization_interests_attributes: [:interest_ids],
attachments_attributes: [:id, :file, :_destroy],
interest_ids: [], registered_lobby_ids: [])
end

Expand Down
4 changes: 4 additions & 0 deletions app/helpers/admin/organizations_helper.rb
Expand Up @@ -12,5 +12,9 @@ def category_name(id)
def show_partial?(partial)
params[:show] ? params[:show] == partial && current_user.lobby? : false
end

def organization_attachments_download_dropdown_id(organization)
"organization_#{organization.id}_attachments_dropdown"
end
end
end
1 change: 1 addition & 0 deletions app/models/attachment.rb
Expand Up @@ -21,6 +21,7 @@ class Attachment < ActiveRecord::Base

belongs_to :event
belongs_to :agent
belongs_to :organization

after_validation :cleanup_paperclip_duplicate_errors

Expand Down
2 changes: 2 additions & 0 deletions app/models/organization.rb
Expand Up @@ -18,6 +18,7 @@ class Organization < ActiveRecord::Base
has_many :events
has_many :organization_registered_lobbies, dependent: :destroy
has_many :registered_lobbies, through: :organization_registered_lobbies, dependent: :destroy
has_many :attachments, dependent: :destroy
has_one :comunication_representant, dependent: :destroy
has_one :user, dependent: :destroy
has_one :legal_representant, dependent: :destroy
Expand All @@ -27,6 +28,7 @@ class Organization < ActiveRecord::Base
accepts_nested_attributes_for :user
accepts_nested_attributes_for :represented_entities, allow_destroy: true
accepts_nested_attributes_for :registered_lobbies, allow_destroy: true, reject_if: :all_blank
accepts_nested_attributes_for :attachments, allow_destroy: true, reject_if: :all_blank

before_create :set_inscription_date
before_validation :invalidate_organization, :validate_organization
Expand Down
16 changes: 16 additions & 0 deletions app/views/admin/organizations/_attachment_fields.html.erb
@@ -0,0 +1,16 @@
<div class="nested-fields small-12 columns">
<div class="row">
<div class="small-2columns left">
<%= f.hidden_field :_destroy %>
<% if f.object.file.present? && f.object.errors.empty? %>
<%= link_to t('backend.open_file'), f.object.file.url %>
<% else %>
<%= f.file_field :file, class: "attachment" %>
<% end %>
</div>
<div class="small-3 columns left">
<%= link_to_remove_association t('backend.organization.remove_attachment'), f,
class: "button tiny radius alert right" %>
</div>
</div>
</div>
21 changes: 21 additions & 0 deletions app/views/admin/organizations/_attachments.html.erb
@@ -0,0 +1,21 @@
<fieldset>
<legend><%= t('backend.attachments') %></legend>
<div class="row">
<div id="nested-attachments">
<%= f.fields_for :attachments do |attachments_builder| %>
<%= render 'attachment_fields', f: attachments_builder %>
<% end %>
</div>
<div class="small-12 columns">
<%= link_to_add_association t('backend.organization.add_attachment'), f,
:attachments,
partial: "attachment_fields",
id: "attachments_link",
class: "button tiny radius info",
data: {
association_insertion_node: "#nested-attachments",
association_insertion_method: "append"
} %>
</div>
</div>
</fieldset>
23 changes: 12 additions & 11 deletions app/views/admin/organizations/_form.html.erb
@@ -1,16 +1,17 @@
<%= form_for [:admin, @organization], html: { class: 'organization-form' } do |f| %>
<%= render 'organization_errors', f: f if current_user.admin? %>
<%= render 'reference', f: f if current_user.admin? %>
<%= render 'data', f: f if current_user.admin? %>
<%= render 'legal_representant', f: f if current_user.admin? %>
<%= render 'user', f: f if current_user.admin? %>
<%= render 'lobby_data', f: f if current_user.admin? %>
<%= render 'represented_entities', f: f if current_user.admin? %>
<%= render 'interests', f: f if current_user.admin? || show_partial?('interests') %>
<%= render 'agents', organization: @organization if current_user.admin? %>
<%= render 'terms', f: f if current_user.admin? %>
<%= render 'invalidate', f: f if current_user.admin? %>
<%= render 'organization_errors', f: f %>
<%= render 'reference', f: f %>
<%= render 'data', f: f %>
<%= render 'legal_representant', f: f %>
<%= render 'user', f: f %>
<%= render 'lobby_data', f: f %>
<%= render 'represented_entities', f: f %>
<%= render 'interests', f: f %>
<%= render 'agents', organization: @organization %>
<%= render 'terms', f: f %>
<%= render 'attachments', f: f %>
<%= render 'invalidate', f: f %>

<div class="small-4 medium-3 large-2 column">
<%= submit_tag t('backend.save'), class: "button radius success right" %>
Expand Down
22 changes: 22 additions & 0 deletions app/views/admin/organizations/index.html.erb
Expand Up @@ -56,6 +56,28 @@
title: t('backend.delete') do %>
<i class="step fi-page-delete size-24"></i>
<% end if can? :destroy, organization %>
<% if organization.attachments.any? %>
<%= link_to "#", title: t('backend.attachments_download'),
class: "organization-attachments-dropdown-link",
data: { dropdown: "#{organization_attachments_download_dropdown_id(organization)}" },
aria: { controls: "#{organization_attachments_download_dropdown_id(organization)}" } do %>
<i class="step fi-archive size-24"></i>
<% end %>

<ul id="<%= organization_attachments_download_dropdown_id(organization) %>"
class="organization-attachments-dropdown f-dropdown"
data-dropdown-content aria-hidden="true" tabindex="-1">
<% organization.attachments.each do |attachment| %>
<li>
<%= link_to attachment.file.url, target: :_blank do %>
<i class="step fi-archive size-24"></i>
<%= attachment.file.original_filename %>
<% end %>
</li>
<% end %>
</ul>
<% end %>
</td>
</tr>
<% end %>
Expand Down
4 changes: 4 additions & 0 deletions config/locales/organizations.es.yml
Expand Up @@ -87,6 +87,10 @@ es:
placeholder:
phones: "Puede añadir más de un teléfono"

organization:
add_attachment: Añadir archivo adjunto
remove_attachment: Eliminar archivo adjunto

identifying_data:
title_fieldset: "Datos identificativos"
identifier: "DNI/NIF/NIE/Pasaporte"
Expand Down
@@ -0,0 +1,5 @@
class AddOrganizationIdToAttachments < ActiveRecord::Migration
def change
add_reference :attachments, :organization, index: true, foreign_key: true
end
end
5 changes: 4 additions & 1 deletion db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20171223192052) do
ActiveRecord::Schema.define(version: 20171226135403) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -74,10 +74,12 @@
t.text "description"
t.boolean "public"
t.integer "agent_id"
t.integer "organization_id"
end

add_index "attachments", ["agent_id"], name: "index_attachments_on_agent_id", using: :btree
add_index "attachments", ["event_id"], name: "index_attachments_on_event_id", using: :btree
add_index "attachments", ["organization_id"], name: "index_attachments_on_organization_id", using: :btree

create_table "attendees", force: :cascade do |t|
t.string "name"
Expand Down Expand Up @@ -350,6 +352,7 @@

add_foreign_key "agents", "organizations"
add_foreign_key "attachments", "events"
add_foreign_key "attachments", "organizations"
add_foreign_key "attendees", "events"
add_foreign_key "event_agents", "events"
add_foreign_key "event_represented_entities", "events"
Expand Down
14 changes: 14 additions & 0 deletions lib/tasks/organizations.rake
Expand Up @@ -45,4 +45,18 @@ namespace :organizations do
end
end

desc "Add new registered_lobbies to database if they do not exists"
task :add_registered_lobbies => :environment do
registered_lobbies = ['no_record',
'generalitat_catalunya',
'cnmc',
'europe_union',
'others']


registered_lobbies.each do |name|
RegisteredLobby.find_or_create_by(name: name)
end
end

end
113 changes: 100 additions & 13 deletions spec/features/admin/organizations_spec.rb
@@ -1,9 +1,10 @@
feature 'Organization' do

describe 'User admin' do

background do
user_admin = create(:user, :admin)
signin(user_admin.email, user_admin.password)
login_as user_admin
end

scenario 'Visit admin page and display organization button on sidebar' do
Expand All @@ -14,9 +15,7 @@

describe "Index" do

background do
3.times { create(:organization) }
end
let!(:organizations) { create_list(:organization, 3) }

describe "Search form", :search do

Expand Down Expand Up @@ -105,6 +104,20 @@

end

scenario "Should display attachments downloads menu dropdown when organization has any attachment", :js, :search do
organization = organizations.first
create(:attachment, organization: organization)
Organization.reindex
visit admin_organizations_path

expect(page).to have_selector ".organization-attachments-dropdown", visible: false
within "#organization_#{organization.id}" do
find(".organization-attachments-dropdown-link").click

expect(page).to have_selector ".organization-attachments-dropdown", visible: true
end
end

scenario 'visit admin page and organization button render organization index', :search do
Organization.reindex
visit admin_path
Expand Down Expand Up @@ -170,19 +183,38 @@
expect(page).to have_button "Guardar"
end

scenario 'Should not display agents link new organization records' do
visit new_admin_organization_path
describe "Agents" do
scenario 'Should not display agents link new organization records' do
visit new_admin_organization_path

expect(page).not_to have_content "Añadir Agentes"
end

expect(page).not_to have_content "Añadir Agentes"
scenario 'Should display agents notice for new organization records' do
visit new_admin_organization_path

expect(page).to have_content "Podrás crear agentes una vez hayas creado " \
"el lobby. Completa el formulario y pulsa " \
"'Guardar', una vez almacenado se habilitará " \
"la opción para añadir agentes."
end
end

scenario 'Should display agents notice for new organization records' do
visit new_admin_organization_path
describe "Documents" do
scenario 'Should display add attachment link' do
visit new_admin_organization_path

expect(page).to have_content "Podrás crear agentes una vez hayas creado " \
"el lobby. Completa el formulario y pulsa " \
"'Guardar', una vez almacenado se habilitará " \
"la opción para añadir agentes."
expect(page).to have_content "Archivos adjuntos"
expect(page).to have_link "Añadir archivo adjunto"
end

scenario 'Should add new attachment file attribute after click "Añadir archivo adjunto" link', :js do
visit new_admin_organization_path

expect(page).not_to have_selector "input[type=file]"
click_link "Añadir archivo adjunto"
expect(page).to have_selector "input[type=file]"
end
end
end

Expand Down Expand Up @@ -355,6 +387,7 @@
expect(page).to have_content "European Country"
expect(page).to have_content "Local Lobby"
end

describe "Nested fields" do

describe "Legal Representant" do
Expand Down Expand Up @@ -502,6 +535,28 @@
end
end

describe "Documents" do
scenario 'Create organization with valid document', :js do
category = create(:category)
visit new_admin_organization_path
fill_in :organization_name, with: "organization name"
select category.name, from: :organization_category_id
fill_in :organization_user_attributes_first_name, with: "user first name"
fill_in :organization_user_attributes_last_name, with: "user last name"
fill_in :organization_user_attributes_email, with: "user@email.com"
fill_in :organization_user_attributes_password, with: "password"
fill_in :organization_user_attributes_password_confirmation, with: "password"

click_link "Añadir archivo adjunto"
within "#nested-attachments" do
attach_file find("input[type='file']")[:id], "spec/fixtures/dummy.pdf"
end
click_button "Guardar"

expect(page).to have_content "Registro creado correctamente"
expect(Organization.last.attachments.count).to eq(1)
end
end
end

scenario "Shouldn't show invalidate button" do
Expand Down Expand Up @@ -853,6 +908,38 @@

end

describe "Documents" do
scenario 'Should display add attachment link' do
organization = create(:organization)
visit edit_admin_organization_path(organization)

expect(page).to have_content "Archivos adjuntos"
expect(page).to have_link "Añadir archivo adjunto"
end

scenario 'Should add new attachment file attribute after click "Añadir archivo adjunto" link', :js do
organization = create(:organization)
visit edit_admin_organization_path(organization)

expect(page).not_to have_selector "input[type=file]"
click_link "Añadir archivo adjunto"
expect(page).to have_selector "input[type=file]"
end

scenario 'Should remove destroyed attachments', :js do
organization = create(:organization)
create(:attachment, organization: organization)
visit edit_admin_organization_path(organization)

click_link "Eliminar archivo adjunto"
click_on "Guardar"
visit edit_admin_organization_path(organization)

expect(page).not_to have_link "Eliminar archivo adjunto"
end

end

end

end
Expand Down

0 comments on commit 603b0f5

Please sign in to comment.