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

More cleanup #473

Merged
merged 7 commits into from
Dec 8, 2023
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
333 changes: 171 additions & 162 deletions Gemfile.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/admin/observation_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def apply_filtering(chain)
end
column :observers do |o|
links = []
o.observers.joins(:translations).each do |observer|
o.observers.joins(:translations).find_each do |observer|
links << link_to(observer.name, admin_monitor_path(observer.id))
end
links.reduce(:+)
Expand Down
2 changes: 1 addition & 1 deletion app/admin/observation_reports_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
as: :select,
multiple: true,
label: -> { I18n.t("activerecord.models.observer") },
collection: -> { Observer.where(id: ObservationReportStatistic.all.select(:observer_id).distinct).order(:name) }
collection: -> { Observer.where(id: ObservationReportStatistic.select(:observer_id).distinct).order(:name) }
filter :date

dependent_filters do
Expand Down
2 changes: 0 additions & 2 deletions app/admin/required_gov_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

filter :required_gov_document_group, collection: -> {
RequiredGovDocumentGroup
.all
.left_joins(:parent)
.order(Arel.sql("coalesce(parents_required_gov_document_groups.position, required_gov_document_groups.position)"))
}
Expand Down Expand Up @@ -60,7 +59,6 @@ def scoped_collection
f.inputs "Required Gov Document Details" do
editing = !object.new_record?
f.input :required_gov_document_group, collection: RequiredGovDocumentGroup
.all
.left_joins(:parent)
.order(Arel.sql("coalesce(parents_required_gov_document_groups.position, required_gov_document_groups.position)"))
f.input :country, input_html: {disabled: editing}
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ def api_auth
end

def api_key_present?
!!request.env.fetch("HTTP_OTP_API_KEY", "").scan(/Bearer/).flatten.first
!!request.env.fetch("HTTP_OTP_API_KEY", "").scan("Bearer").flatten.first
end

def auth_present?
!!request.env.fetch("HTTP_AUTHORIZATION", "").scan(/Bearer/).flatten.first
!!request.env.fetch("HTTP_AUTHORIZATION", "").scan("Bearer").flatten.first
end

def bad_auth_key
Expand Down
53 changes: 0 additions & 53 deletions app/models/comment.rb

This file was deleted.

2 changes: 2 additions & 0 deletions app/models/gov_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ class GovDocument < ApplicationRecord

after_update :move_previous_attachment_to_private_directory, if: :saved_change_to_attachment?

skip_callback :commit, :after, :remove_attachment!
after_destroy :move_attachment_to_private_directory
after_restore :move_attachment_to_public_directory
after_real_destroy :remove_attachment!

scope :with_archived, -> { unscope(where: :deleted_at) }
scope :to_expire, ->(date) { where("expire_date < ?", date).where(status: EXPIRABLE_STATUSES) }
Expand Down
10 changes: 0 additions & 10 deletions app/models/government.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,6 @@ class Government < ApplicationRecord

default_scope { includes(:translations) }

class << self
def fetch_all(options)
country_id = options["country"] if options.present? && options["country"].present?

governments = includes(:country)
governments = governments.filter_by_country(country_id) if country_id.present?
governments
end
end

alias_method :to_s, :government_entity

def cache_key
Expand Down
1 change: 0 additions & 1 deletion app/models/observation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ class Observation < ApplicationRecord
has_many :observation_operators, dependent: :destroy
has_many :relevant_operators, through: :observation_operators, source: :operator

has_many :comments, as: :commentable, dependent: :destroy
has_many :observation_documents, dependent: :destroy

accepts_nested_attributes_for :observation_documents, allow_destroy: true
Expand Down
2 changes: 2 additions & 0 deletions app/models/observation_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class ObservationDocument < ApplicationRecord
belongs_to :user, inverse_of: :observation_documents, touch: true, optional: true
belongs_to :observation, inverse_of: :observation_documents, touch: true

skip_callback :commit, :after, :remove_attachment!
after_destroy :move_attachment_to_private_directory
after_restore :move_attachment_to_public_directory
after_real_destroy :remove_attachment!
end
2 changes: 2 additions & 0 deletions app/models/observation_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ class ObservationReport < ApplicationRecord
has_many :observers, through: :observation_report_observers
has_many :observations, dependent: :destroy

skip_callback :commit, :after, :remove_attachment!
after_destroy :move_attachment_to_private_directory
after_restore :move_attachment_to_public_directory
after_real_destroy :remove_attachment!

scope :bigger_date, ->(date) { where("observation_reports.created_at <= ?", date + 1.day) }

Expand Down
4 changes: 0 additions & 4 deletions app/models/observer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ class Observer < ApplicationRecord
default_scope { includes(:translations) }

class << self
def fetch_all(options)
includes(:countries, :users)
end

def observer_select
by_name_asc.map { |c| ["#{c.name} (#{c.observer_type})", c.id] }
end
Expand Down
12 changes: 2 additions & 10 deletions app/models/operator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,6 @@ class Operator < ApplicationRecord
}

class << self
def fetch_all(options)
country_ids = options["country_ids"] if options.present? && options["country_ids"].present?

operators = includes(:country, :users)
operators = operators.filter_by_country_ids(country_ids) if country_ids.present?
operators
end

def operator_select
by_name_asc.map { |c| [c.name, c.id] }
end
Expand Down Expand Up @@ -156,8 +148,8 @@ def recalculate_scores

def clean_document_cache
# TODO: try different technique for jsonapi cache invalidation, this is undocumented way for cleaning cache of jsonapi resources
Rails.cache.delete_matched(/operator_documents\/(#{operator_document_ids.join('|')})\//)
Rails.cache.delete_matched(/operator_document_histories\/(#{operator_document_history_ids.join('|')})\//)
Rails.cache.delete_matched(/operator_documents\/(#{operator_document_ids.join("|")})\//)
Rails.cache.delete_matched(/operator_document_histories\/(#{operator_document_history_ids.join("|")})\//)
end

def refresh_ranking
Expand Down
6 changes: 0 additions & 6 deletions app/models/species.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ class Species < ApplicationRecord

default_scope { includes(:translations) }

class << self
def fetch_all(options)
includes(:countries)
end
end

def cache_key
super + "-" + Globalize.locale.to_s
end
Expand Down
11 changes: 2 additions & 9 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class User < ApplicationRecord
has_one :api_key, dependent: :destroy
has_one :user_permission
has_many :observations, inverse_of: :user
has_many :comments, inverse_of: :user, dependent: :destroy
has_many :observation_documents, inverse_of: :user
has_many :observation_reports, inverse_of: :user
has_many :operator_document_annexes, inverse_of: :user
Expand Down Expand Up @@ -79,12 +78,6 @@ class User < ApplicationRecord
scope :inactive, -> { where(is_active: false) }
scope :with_user_role, ->(role) { joins(:user_permission).where(user_permission: {user_role: role}) }

class << self
def fetch_all(options)
includes(:user_permission, :comments, :country)
end
end

def is_government(country_id)
self&.user_permission&.user_role == "government" && self.country_id == country_id
end
Expand Down Expand Up @@ -210,7 +203,7 @@ def notify_user
end

# Devise ActiveJob integration
def send_devise_notification(notification, *args)
devise_mailer.send(notification, self, *args).deliver_later
def send_devise_notification(notification, *)
devise_mailer.send(notification, self, *).deliver_later
end
end
9 changes: 9 additions & 0 deletions app/resources/v1/base_resource.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
module V1
class BaseResource < JSONAPI::Resource
abstract

def observations_tool_user?
context[:app] == "observations-tool" && context[:current_user].present? &&
["ngo", "ngo_manager", "admin"].include?(context[:current_user].user_permission.user_role)
end

def admin_user?
context[:current_user].present? && context[:current_user].user_permission.user_role == "admin"
end
end
end
9 changes: 0 additions & 9 deletions app/resources/v1/comment_resource.rb

This file was deleted.

3 changes: 1 addition & 2 deletions app/resources/v1/observation_document_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ class ObservationDocumentResource < BaseResource
attributes :name, :attachment, :created_at, :updated_at

has_one :observation
has_one :user

filters :observation_id, :name, :user_id
filters :observation_id, :name
end
end
7 changes: 6 additions & 1 deletion app/resources/v1/observation_report_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class ObservationReportResource < BaseResource
attributes :title, :publication_date, :created_at, :updated_at, :attachment

has_many :observers
has_one :user
has_many :observations

after_create :add_observers
Expand All @@ -19,5 +18,11 @@ class ObservationReportResource < BaseResource
def add_observers
@model.update_observers if @model.observations.any?
end

def fetchable_fields
return super if observations_tool_user?

super - [:created_at, :updated_at, :user]
end
end
end
7 changes: 6 additions & 1 deletion app/resources/v1/observation_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class ObservationResource < BaseResource
:is_physical_place, :complete, :hidden, :admin_comment, :monitor_comment

has_many :species
has_many :comments
has_many :observation_documents
has_many :observers
has_many :relevant_operators, class_name: "Operator"
Expand Down Expand Up @@ -70,6 +69,12 @@ class ObservationResource < BaseResource
records.where(id: records.joins(:observers).where(observers: {id: value}).pluck(:id))
}

def fetchable_fields
return super if observations_tool_user?

super - [:admin_comment, :monitor_comment, :created_at, :updated_at, :user, :modified_user]
end

def self.sortable_fields(context)
super + [:"country.iso", :"severity.level", :"subcategory.name",
:"operator.name", :"country.name", :"law.written_infraction",
Expand Down
1 change: 0 additions & 1 deletion app/resources/v1/observer_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class ObserverResource < BaseResource
:data_email, :data_phone, :organization_type, :delete_logo, :public_info

has_many :countries
has_many :users
has_many :observations

filters :countries, :is_active
Expand Down
1 change: 0 additions & 1 deletion app/resources/v1/operator_document_annex_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class OperatorDocumentAnnexResource < BaseResource
:uploaded_by, :created_at, :updated_at

has_one :operator_document, foreign_key_on: :related
has_one :user

filters :status

Expand Down
2 changes: 1 addition & 1 deletion app/resources/v1/operator_document_history_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def self.records(options = {})

OperatorDocumentHistory.from_operator_at_date(operator, date).non_signature
rescue
return OperatorDocumentHistory.where("true = false") unless operator && date
OperatorDocumentHistory.where("true = false") unless operator && date
end
end
end
1 change: 0 additions & 1 deletion app/resources/v1/operator_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class OperatorResource < BaseResource

has_one :country
has_many :fmus
has_many :users
has_many :observations
has_many :sawmills

Expand Down
18 changes: 17 additions & 1 deletion app/resources/v1/user_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class UserResource < BaseResource

has_one :country
has_one :user_permission, foreign_key_on: :related
has_many :comments
has_many :managed_observers, class_name: "Observer"
has_one :observer
has_one :operator
Expand All @@ -19,5 +18,22 @@ def self.fields
end

filters :is_active, :email, :name

def fetchable_fields
return super if owner? || admin_user?
return [:id, :name, :email] if password_reset_action?

[:id, :name]
end

private

def password_reset_action?
context[:action] == "update" && context[:controller] == "v1/passwords"
end

def owner?
context[:current_user].present? && context[:current_user].id == @model.id
end
end
end
2 changes: 1 addition & 1 deletion app/uploaders/application_uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def cache_dir
end

def url(*args)
return "/private#{super}" if private_upload?
return super.gsub("/uploads/", "/private/uploads/") if private_upload?

super
end
Expand Down
4 changes: 2 additions & 2 deletions config/initializers/enum_attributes_validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def validate_enum_attributes(*attributes, **opts)
end
end

def validate_enum_attribute(*attributes)
validate_enum_attributes(*attributes)
def validate_enum_attribute(*)
validate_enum_attributes(*)
end
end
end
Expand Down
Loading