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/government removal #36

Merged
merged 2 commits into from
Dec 21, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 7 additions & 2 deletions app/admin/government.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@

config.order_clause

actions :all, except: :destroy
permit_params :country_id, translations_attributes: [:id, :locale, :government_entity, :details, :_destroy]
actions :all
permit_params :country_id, :is_active, translations_attributes: [:id, :locale, :government_entity, :details, :_destroy]

controller do
def scoped_collection
end_of_association_chain.includes([:translations, [country: :translations]])
end
end

scope :all
scope :active, default: true

index do
column 'Active?', :is_active
column :country, sortable: 'country_translations.name'
column :government_entity, sortable: 'government_translations.government_entity'
column :details, sortable: 'government_translations.government_details'
Expand Down Expand Up @@ -43,6 +46,7 @@ def scoped_collection
edit = f.object.new_record? ? false : true
f.semantic_errors *f.object.errors.keys
f.inputs 'Government Details' do
f.input :is_active
f.input :country, input_html: { disabled: edit }
end

Expand All @@ -57,6 +61,7 @@ def scoped_collection

show title: proc{ "#{resource.government_entity}" }do
attributes_table do
row :is_active
row :country
row :government_entity
row :details
Expand Down
12 changes: 12 additions & 0 deletions app/models/government.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ class Government < ApplicationRecord
has_many :observations, inverse_of: :government

validates :government_entity, presence: true
before_destroy :observations_present?

scope :by_entity_asc, -> {
includes(:translations).with_translations(I18n.available_locales)
.order('government_translations.government_entity ASC')
}

scope :filter_by_country, ->(country_id) { where(country_id: country_id) }
scope :active, ->() { where(is_active: true) }

default_scope { includes(:translations) }

Expand All @@ -42,4 +44,14 @@ def fetch_all(options)
def cache_key
super + '-' + Globalize.locale.to_s
end

private

# Doesn't allow the removal of the entity if there are observations for it
def observations_present?
if observations.any?
errors.add(:base, 'Cannot delete an entity that has observations.')
false
end
end
end
8 changes: 8 additions & 0 deletions app/models/observation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class Observation < ApplicationRecord
validates :country_id, presence: true
validates :publication_date, presence: true
validates_presence_of :validation_status
validate :active_government

before_save :set_active_status
after_create :update_operator_scores
Expand Down Expand Up @@ -129,4 +130,11 @@ def set_active_status
def destroy_documents
observation_documents.find_each(&:really_destroy!)
end

def active_government
return if observation_type != 'government'
return if persisted?
return if government.nil?
errors[:government] << 'The selected government is not active' unless government.is_active
end
end
4 changes: 2 additions & 2 deletions app/resources/v1/government_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ module V1
class GovernmentResource < JSONAPI::Resource
caching

attributes :government_entity, :details
attributes :government_entity, :details, :is_active

has_one :country

def self.sortable_fields(context)
super + [:'country.name']
end

filters :country
filters :country, :is_active

filter :'country.name', apply: ->(records, value, _options) {
if value.present?
Expand Down
7 changes: 4 additions & 3 deletions 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: 20171218115838) do
ActiveRecord::Schema.define(version: 20171220163723) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -150,8 +150,9 @@

create_table "governments", force: :cascade do |t|
t.integer "country_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "is_active", default: true
t.index ["country_id"], name: "index_governments_on_country_id", using: :btree
end

Expand Down