-
Notifications
You must be signed in to change notification settings - Fork 3
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
Feature/events #36
Changes from 6 commits
08f2c52
642dc5f
1ed81bb
8328d6f
54f03e3
4749546
2edbe88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here I would just put There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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 %> |
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> |
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 |
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 |
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 |
There was a problem hiding this comment.
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?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok