Skip to content

Commit

Permalink
Merge branch 'tool-collections' into stagingTmp
Browse files Browse the repository at this point in the history
  • Loading branch information
frett committed Jul 25, 2023
2 parents cfa2788 + 6d25bd5 commit 4a13021
Show file tree
Hide file tree
Showing 36 changed files with 1,372 additions and 3 deletions.
10 changes: 10 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,14 @@ def initialize(code, message)
errors.add(code, message)
end
end

def convert_hyphen_to_dash
params.deep_transform_keys! { |key| key.tr("-", "_") }
end

def formatted_errors(error)
error.record.errors.map do |attribute, errors|
errors.map { |error_message| {detail: "#{attribute} #{error_message}"} }
end.flatten
end
end
24 changes: 24 additions & 0 deletions app/controllers/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,30 @@ def push_to_onesky
head :no_content
end

def suggestions
resources_1 = ToolGroup
.matching_countries__negative_rule_false(params["country"])
.matching_languages__negative_rule_false(params["languages"])
.joins(:rule_countries, :rule_languages)

resources_2 = ToolGroup
.countries_not_matching__negative_rule_true(params["country"])
.matching_languages__negative_rule_false(params["languages"])
.joins(:rule_countries, :rule_languages)

resources_3 = ToolGroup
.matching_countries__negative_rule_false(params["country"])
.languages_not_matching__negative_rule_true(params["languages"])
.joins(:rule_countries, :rule_languages)

resources_4 = ToolGroup
.countries_not_matching__negative_rule_true(params["country"])
.matching_languages__negative_rule_false(params["languages"])
.joins(:rule_countries, :rule_languages)

render json: resources_1 + resources_2 + resources_3 + resources_4, status: :ok
end

private

def cached_index_json
Expand Down
36 changes: 36 additions & 0 deletions app/controllers/rule_countries_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class RuleCountriesController < ApplicationController
before_action :authorize!

def create
create_rule_country
rescue ActiveRecord::RecordInvalid => e
render json: {errors: formatted_errors(e)}, status: :unprocessable_entity
end

def update
update_rule_country
end

def destroy
tool_group = ToolGroup.find(params[:tool_group_id])
rule_country = tool_group.rule_countries.find(params[:id])
rule_country.destroy!
head :no_content
end

private

def create_rule_country
tool_group = ToolGroup.find(params[:tool_group_id])
created = tool_group.rule_countries.create!(permit_params(:tool_group_id, :negative_rule, countries: []))
response.headers["Location"] = "tool_groups/#{created.id}"
render json: created, status: :created
end

def update_rule_country
tool_group = ToolGroup.find(params[:tool_group_id])
existing = tool_group.rule_countries.find(params[:id])
existing.update!(permit_params(:negative_rule, countries: []))
render json: existing, status: :accepted
end
end
37 changes: 37 additions & 0 deletions app/controllers/rule_languages_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class RuleLanguagesController < ApplicationController
before_action :authorize!
before_action :convert_hyphen_to_dash, only: [:create, :update]

def create
create_rule_language
rescue ActiveRecord::RecordInvalid => e
render json: {errors: formatted_errors(e)}, status: :unprocessable_entity
end

def update
update_rule_language
end

def destroy
tool_group = ToolGroup.find(params[:tool_group_id])
rule_language = tool_group.rule_languages.find(params[:id])
rule_language.destroy!
head :no_content
end

private

def create_rule_language
tool_group = ToolGroup.find(params[:tool_group_id])
created = tool_group.rule_languages.create!(permit_params(:tool_group_id, :negative_rule, languages: []))
response.headers["Location"] = "tool_groups/#{created.id}"
render json: created, status: :created
end

def update_rule_language
tool_group = ToolGroup.find(params[:tool_group_id])
existing = tool_group.rule_languages.find(params[:id])
existing.update!(permit_params(:negative_rule, languages: []))
render json: existing, status: :accepted
end
end
36 changes: 36 additions & 0 deletions app/controllers/rule_praxes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class RulePraxesController < ApplicationController
before_action :authorize!

def create
create_rule_praxis
rescue ActiveRecord::RecordInvalid => e
render json: {errors: formatted_errors(e)}, status: :unprocessable_entity
end

def update
update_rule_praxis
end

def destroy
tool_group = ToolGroup.find(params[:tool_group_id])
rule_praxes = tool_group.rule_praxes.find(params[:id])
rule_praxes.destroy!
head :no_content
end

private

def create_rule_praxis
tool_group = ToolGroup.find(params[:tool_group_id])
created = tool_group.rule_praxes.create!(permit_params(:tool_group_id, :negative_rule, openness: [], confidence: []))
response.headers["Location"] = "tool_groups/#{created.id}"
render json: created, status: :created
end

def update_rule_praxis
tool_group = ToolGroup.find(params[:tool_group_id])
existing = tool_group.rule_praxes.find(params[:id])
existing.update!(permit_params(:negative_rule, openness: [], confidence: []))
render json: existing, status: :accepted
end
end
79 changes: 79 additions & 0 deletions app/controllers/tool_groups_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
class ToolGroupsController < ApplicationController
before_action :authorize!
before_action :convert_hyphen_to_dash, only: [:create, :update]

def index
render json: tool_groups_ordered_by_name, include: params[:include], fields: field_params, status: :ok
end

def create
create_tool_group
rescue ActiveRecord::RecordInvalid => e
render json: {errors: formatted_errors(e)}, status: :unprocessable_entity
end

def create_tool
ResourceToolGroup.create!(
tool_group_id: params[:tool_group_id],
resource_id: params[:data][:attributes]["resource-id"],
suggestions_weight: params[:data][:attributes]["suggestions-weight"]
)

tool_group = ToolGroup.find(params[:tool_group_id])
response.headers["Location"] = "tool_groups/#{tool_group.id}"
render json: tool_group, status: :created
rescue ActiveRecord::RecordInvalid => e
render json: {errors: formatted_errors(e)}, status: :unprocessable_entity
end

def update_tool
existing = ResourceToolGroup.find(params[:id])
existing.update!(
resource_id: params[:data][:attributes]["resource-id"],
suggestions_weight: params[:data][:attributes]["suggestions-weight"]
)
render json: existing, status: :accepted
end

def delete_tool
resource_tool_group = ResourceToolGroup.find(params[:id])
resource_tool_group.destroy!
head :no_content
end

def show
render json: load_tool_group, include: params[:include], fields: field_params, status: :ok
end

def destroy
tool_group = ToolGroup.find(params[:id])
tool_group.destroy!
head :no_content
end

def update
update_tool_group
end

private

def tool_groups_ordered_by_name
ToolGroup.order(name: :asc)
end

def create_tool_group
created = ToolGroup.create!(permit_params(:name, :suggestions_weight))
response.headers["Location"] = "tool_groups/#{created.id}"
render json: created, status: :created
end

def update_tool_group
existing = ToolGroup.find(params[:id])
existing.update!(permit_params(:name, :suggestions_weight))
render json: existing, status: :accepted
end

def load_tool_group
ToolGroup.find(params[:id])
end
end
3 changes: 3 additions & 0 deletions app/models/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class Resource < ActiveRecord::Base
has_many :translated_pages
has_many :translated_attributes
has_many :custom_manifests
has_many :resource_tool_groups
has_many :tool_groups, through: :resource_tool_groups

belongs_to :metatool, optional: true, class_name: "Resource"
belongs_to :default_variant, optional: true, class_name: "Resource"
has_many :variants, class_name: "Resource", foreign_key: :metatool_id
Expand Down
7 changes: 7 additions & 0 deletions app/models/resource_tool_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

# app/models/resource_tool_group.rb
class ResourceToolGroup < ApplicationRecord
belongs_to :resource
belongs_to :tool_group
end
17 changes: 17 additions & 0 deletions app/models/rule_country.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class RuleCountry < ApplicationRecord
belongs_to :tool_group

validates :tool_group_id, uniqueness: {scope: [:countries, :negative_rule], message: "combination already exists"}
validate :validate_countries

private

def validate_countries
countries.each do |country|
unless country.match?(/\A[A-Z]{2}\z/)
errors.add(:countries, "must contain only ISO-3166 alpha-2 country codes")
break
end
end
end
end
5 changes: 5 additions & 0 deletions app/models/rule_language.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RuleLanguage < ApplicationRecord
belongs_to :tool_group

validates :tool_group_id, uniqueness: {scope: [:languages, :negative_rule], message: "combination already exists"}
end
26 changes: 26 additions & 0 deletions app/models/rule_praxis.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class RulePraxis < ApplicationRecord
self.table_name = "rule_praxes"

belongs_to :tool_group

validates :tool_group_id, uniqueness: {scope: [:openness, :confidence], message: "combination already exists"}
validate :validate_openness_or_confidence, if: :no_openness_or_confidence?

validates_each :openness, :confidence do |record, attr, value|
next if value.blank?

value.each do |v|
record.errors.add(attr, "must contain integer values between 1 and 5 or an empty array") unless v.is_a?(Integer) && (1..5).cover?(v)
end
end

private

def no_openness_or_confidence?
openness.blank? && confidence.blank?
end

def validate_openness_or_confidence
errors.add(:base, "Either 'openness' or 'confidence' must be present")
end
end
31 changes: 31 additions & 0 deletions app/models/tool_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

# ToolGroup model class
class ToolGroup < ApplicationRecord
validates :name, :suggestions_weight, presence: true
validates :name, uniqueness: true

has_many :rule_languages, dependent: :destroy
has_many :rule_countries, dependent: :destroy
has_many :rule_praxes, class_name: "RulePraxis", dependent: :destroy
has_many :resource_tool_groups
has_many :resources, through: :resource_tool_groups

scope :matching_countries__negative_rule_false, lambda { |country|
where("countries @> ARRAY[?]::varchar[] AND rule_countries.negative_rule = ?", country&.upcase, false)
}

scope :matching_languages__negative_rule_false, lambda { |language|
where("languages && ARRAY[?]::varchar[] AND rule_languages.negative_rule = ?", language, false)
}

scope :languages_not_matching__negative_rule_true, lambda { |languages|
where("NOT ?::varchar[] <@ languages", "{#{languages.join(',')}}")
.where("rule_languages.negative_rule = ?", true)
}

scope :countries_not_matching__negative_rule_true, lambda { |country|
where.not("countries @> ARRAY[?]::varchar[]", country&.upcase)
.where("rule_countries.negative_rule = ?", true)
}
end
4 changes: 4 additions & 0 deletions app/serializers/resource_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class ResourceSerializer < ActiveModel::Serializer
has_many :custom_manifests, key: "custom-manifests"
has_many :variants, if: -> { object&.resource_type&.name == "metatool" }
has_many :translated_attributes, key: "translated-attributes"

has_many :resource_tool_groups
has_many :tool_groups, through: :resource_tool_groups

belongs_to :default_variant, key: "default-variant", if: -> { object&.resource_type&.name == "metatool" }

def attributes(*args)
Expand Down
11 changes: 11 additions & 0 deletions app/serializers/resource_tool_group_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class ResourceToolGroupSerializer < ActiveModel::Serializer
attributes :resource_id, :tool_group
attribute :suggestions_weight, key: "suggestions-weight"

type "tool-group-tool"

belongs_to :resource
belongs_to :tool_group
end
10 changes: 10 additions & 0 deletions app/serializers/rule_country_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class RuleCountrySerializer < ActiveModel::Serializer
attributes :id, :countries
attribute :negative_rule, key: "negative-rule"

type "tool-group-rule-country"

belongs_to :tool_group
end
10 changes: 10 additions & 0 deletions app/serializers/rule_language_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class RuleLanguageSerializer < ActiveModel::Serializer
attributes :id, :languages
attribute :negative_rule, key: "negative-rule"

type "tool-group-rule-language"

belongs_to :tool_group
end
10 changes: 10 additions & 0 deletions app/serializers/rule_praxis_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class RulePraxisSerializer < ActiveModel::Serializer
attributes :id, :openness, :confidence
attribute :negative_rule, key: "negative-rule"

type "tool-group-rule-praxis"

belongs_to :tool_group
end
Loading

0 comments on commit 4a13021

Please sign in to comment.