-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'tool-collections' into stagingTmp
- Loading branch information
Showing
36 changed files
with
1,372 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.