Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/events #36

Merged
merged 7 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app/admin/litigations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
documents_attributes: [
:id, :_destroy, :name, :language, :external_url, :type, :file
],
events_attributes: [
:id, :_destroy, :title, :event_type, :description, :url, :date
],
legislation_ids: []

filter :title_contains
Expand Down Expand Up @@ -70,6 +73,18 @@
end
end
end

tab :events do
panel 'Litigation Events' do
table_for resource.events.decorate do
column :date
column :event_type
column :title
column :description
column 'URL', &:url_link
end
end
end
end
end

Expand Down
27 changes: 26 additions & 1 deletion app/assets/stylesheets/active_admin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ form {

.inline-fields {
display: flex;
justify-content: space-between;
align-items: center;

li {
Expand Down Expand Up @@ -213,3 +212,29 @@ form.litigation {
width: 70%;
}
}

.event-fields {
.select2 {
width: 100% !important;
}

li[id*=date] {
width: 160px;
}

li[id*=event_type] {
width: 200px;
}

li[id*=_description] {
flex-grow: 1;

textarea {
resize: vertical;
}
}

li[id*=_title], li[id*=_url] {
flex-grow: 1;
}
}
13 changes: 13 additions & 0 deletions app/decorators/event_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class EventDecorator < Draper::Decorator
delegate_all

def url_link
return unless model.url.present?

h.link_to model.url, model.url, target: '_blank'
end

def event_type
model.event_type.titleize
end
end
4 changes: 2 additions & 2 deletions app/helpers/select_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module SelectHelper
def array_to_select_collection(array)
def array_to_select_collection(array, transform_func = :humanize)
return unless array.respond_to?(:map)

array.map { |s| [s.humanize, s] }
array.map { |s| [s.send(transform_func), s] }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a respond_to? check here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

end

def all_languages_to_select_collection
Expand Down
28 changes: 28 additions & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# == Schema Information
#
# Table name: events
#
# id :bigint not null, primary key
# eventable_type :string
# eventable_id :bigint
# title :string not null
# event_type :string not null
# date :date not null
# url :text
# description :text
#

class Event < ApplicationRecord
belongs_to :eventable, polymorphic: true

validates_presence_of :title, :date
validates :url, url: true

validates :event_type, presence: true, inclusion: {in: :valid_types}

def valid_types
return Litigation::EVENT_TYPES if eventable.is_a?(Litigation)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here I would just put eventable.event_types and leave validation for "eventable" model.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validation must be here I guess? but I could add event_types to eventable model sure that's a good idea 👍 but that method must be implemented by all eventables.


[]
end
end
12 changes: 10 additions & 2 deletions app/models/litigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class Litigation < ApplicationRecord
friendly_id :title, use: :slugged, routes: :default

DOCUMENT_TYPES = %w[case investigation inquiry].freeze
EVENT_TYPES = %w[
case_started
case_dismissed
case_overruled
case_decided
].freeze

enum document_type: array_to_enum_hash(DOCUMENT_TYPES)

Expand All @@ -37,10 +43,12 @@ class Litigation < ApplicationRecord
belongs_to :jurisdiction, class_name: 'Geography'
has_many :litigation_sides, -> { order(:side_type) }, inverse_of: :litigation
has_many :documents, as: :documentable, dependent: :destroy
has_many :events, as: :eventable, dependent: :destroy
has_and_belongs_to_many :legislations

accepts_nested_attributes_for :documents, allow_destroy: true
accepts_nested_attributes_for :litigation_sides, allow_destroy: true
accepts_nested_attributes_for :documents, allow_destroy: true, reject_if: :all_blank
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe:

with_options allow_destroy: true, reject_if: :all_blank do
   accepts_nested_attributes_for :documents
   accepts_nested_attributes_for :litigation_sides
   accepts_nested_attributes_for :events
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

accepts_nested_attributes_for :litigation_sides, allow_destroy: true, reject_if: :all_blank
accepts_nested_attributes_for :events, allow_destroy: true, reject_if: :all_blank

validates_presence_of :title, :slug, :document_type
end
13 changes: 13 additions & 0 deletions app/views/admin/events/_fields.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<% is_new_record = form.object.new_record? %>

<%= content_tag :div, class: "nested-fields inline-fields event-fields", data: { new_record: is_new_record } do %>
<%= form.input :date %>
<%= form.input :title %>
<%= form.input :event_type, as: :select, collection: array_to_select_collection(form.object.valid_types, :titleize) %>
<%= form.input :description, as: :text %>
<%= form.input :url, as: :string %>
<li class="input">
<%= button_tag "Remove", type: 'button', class: 'button button--raised button--red', data: { action: "click->nested-list#removeRecord" } %>
</li>
<%= form.hidden_field :_destroy %>
<% end %>
15 changes: 15 additions & 0 deletions app/views/admin/events/_list.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<div class="nested-list" data-controller="nested-list">
<template>
<%= form.semantic_fields_for :events, Event.new(eventable: form.object), child_index: 'NEW_RECORD' do |event| %>
<%= render "admin/events/fields", form: event %>
<% end %>
</template>

<%= form.semantic_fields_for :events do |event| %>
<%= render "admin/events/fields", form: event %>
<% end %>

<div class="nested-list__actions" data-target="nested-list.links">
<%= button_tag "Add Event", type: 'button', class: 'button button--raised', data: { action: "click->nested-list#addRecord" } %>
</div>
</div>
7 changes: 7 additions & 0 deletions app/views/admin/litigations/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<div class="tabs" data-controller="tabs">
<ul class="nav nav-tabs" role="tablist">
<li><a href="#details">Details</a></li>
<li><a href="#events">Events</a></li>
<li><a href="#documents">Documents</a></li>
<li><a href="#sides">Sides</a></li>
</ul>
Expand All @@ -26,6 +27,12 @@
<% end %>
</div>

<div id="events">
<%= f.inputs "List of litigation's events" do %>
<%= render 'admin/events/list', form: f %>
<% end %>
</div>

<div id="documents">
<%= f.inputs 'List of related documents and links' do %>
<%= render 'admin/documents/list', form: f %>
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20190820221054_create_events.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateEvents < ActiveRecord::Migration[5.2]
def change
create_table :events do |t|
t.references :eventable, polymorphic: true, index: true
t.string :title, null: false
t.string :event_type, null: false
t.date :date, null: false
t.text :url
t.text :description
end
end
end
13 changes: 12 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_08_09_131640) do
ActiveRecord::Schema.define(version: 2019_08_20_221054) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -115,6 +115,17 @@
t.index ["documentable_type", "documentable_id"], name: "index_documents_on_documentable_type_and_documentable_id"
end

create_table "events", force: :cascade do |t|
t.string "eventable_type"
t.bigint "eventable_id"
t.string "title", null: false
t.string "event_type", null: false
t.date "date", null: false
t.text "url"
t.text "description"
t.index ["eventable_type", "eventable_id"], name: "index_events_on_eventable_type_and_eventable_id"
end

create_table "geographies", force: :cascade do |t|
t.string "geography_type", null: false
t.string "iso", null: false
Expand Down
36 changes: 31 additions & 5 deletions spec/controllers/admin/litigations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
describe 'POST create' do
context 'with valid params' do
let(:valid_attributes) {
attributes_for(:litigation).merge(
attributes_for(
:litigation,
title: 'Litigation POST title',
summary: 'Litigation POST summary',
core_objective: 'objective',
Expand All @@ -47,16 +48,36 @@
visibility_status: 'pending',
litigation_sides_attributes: [
attributes_for(:litigation_side),
attributes_for(:litigation_side, :company).merge(
attributes_for(
:litigation_side,
:company,
connected_with: "Company-#{side_company.id}"
),
attributes_for(:litigation_side, :geography).merge(
attributes_for(
:litigation_side,
:geography,
connected_with: "Geography-#{side_geography.id}"
)
],
documents_attributes: [
attributes_for(:document).merge(name: 'doc 1'),
attributes_for(:document_uploaded).merge(name: 'doc 2')
attributes_for(:document, name: 'doc 1'),
attributes_for(:document_uploaded, name: 'doc 2')
],
events_attributes: [
{
date: 5.days.ago,
event_type: 'case_started',
title: 'Event 1',
description: 'Description 1',
url: 'https://validurl1.com'
},
{
date: 3.days.ago,
event_type: 'case_dismissed',
title: 'Event 2',
description: 'Description 2',
url: 'https://validurl2.com'
}
]
)
}
Expand All @@ -76,6 +97,11 @@
expect(l.litigation_sides.pluck(:party_type)).to eq(%w[individual corporation government])
expect(l.documents.pluck(:name, :language, :external_url).sort)
.to eq([['doc 1', 'en', 'https://test.com'], ['doc 2', 'en', '']])
expect(l.events.order(:date).pluck(:title, :event_type, :description, :url))
.to eq([
['Event 1', 'case_started', 'Description 1', 'https://validurl1.com'],
['Event 2', 'case_dismissed', 'Description 2', 'https://validurl2.com']
])
end
end

Expand Down
27 changes: 27 additions & 0 deletions spec/factories/events.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# == Schema Information
#
# Table name: events
#
# id :bigint not null, primary key
# eventable_type :string
# eventable_id :bigint
# title :string not null
# event_type :string not null
# date :date not null
# url :text
# description :text
#

FactoryBot.define do
factory :event do
url { 'https://test.com' }
date { 10.days.ago }

factory :litigation_event do
title { 'Some case was started' }
description { 'Description of starting the case' }
event_type { 'case_started' }
eventable { |e| e.association(:litigation) }
end
end
end
48 changes: 48 additions & 0 deletions spec/models/event_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# == Schema Information
#
# Table name: events
#
# id :bigint not null, primary key
# eventable_type :string
# eventable_id :bigint
# title :string not null
# event_type :string not null
# date :date not null
# url :text
# description :text
#

require 'rails_helper'

RSpec.describe Event, type: :model do
subject { build(:event) }

it 'should not be valid without title' do
subject.title = nil
expect(subject).to have(1).errors_on(:title)
end

it 'should not be valid without date' do
subject.date = nil
expect(subject).to have(1).errors_on(:date)
end

it 'should not be valid without event_type' do
subject.event_type = nil
# 2 errors because event type inclusion
expect(subject).to have(2).errors_on(:event_type)
end

describe 'Litigation Event' do
subject { build(:litigation_event) }

it 'should be valid' do
expect(subject).to be_valid
end

it 'should not be valid with wrong type' do
subject.event_type = 'NotListed'
expect(subject).to have(1).errors_on(:event_type)
end
end
end