Skip to content

Commit

Permalink
Merge tool-collections into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
stage-branch-merger[bot] authored Jul 14, 2023
2 parents 80b1ecd + 9cb1449 commit 5e5f3af
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 5 deletions.
33 changes: 33 additions & 0 deletions app/controllers/rule_countries_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class RuleCountriesController < ApplicationController
before_action :authorize!

def create
create_rule_country
rescue ActiveRecord::RecordInvalid => e
render json: {error: e.record.errors}, status: :unprocessable_entity
end

def update
update_rule_country
end

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

private

def create_rule_country
created = RuleCountry.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
existing = RuleCountry.find(params[:id])
existing.update!(permit_params(:negative_rule, :countries => []))
render json: existing, status: :accepted
end
end
3 changes: 3 additions & 0 deletions app/models/rule_country.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class RuleCountry < ApplicationRecord
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
1 change: 1 addition & 0 deletions app/serializers/tool_group_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ class ToolGroupSerializer < ActiveModel::Serializer
type "tool-group"

has_many :rule_languages
has_many :rule_countries
end
8 changes: 7 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@
resources :custom_manifests, only: [:create, :update, :destroy, :show]

resources :tool_groups, path: "tool-groups", only: [:create, :destroy, :index, :show, :update]


# Rule Languages
post "tool-groups/:id/rule-languages", to: "rule_languages#create"
patch "tool-groups/:tool_group_id/rule-languages/:id", to: "rule_languages#update"
delete "tool-groups/:tool_group_id/rule-languages/:id", to: "rule_languages#destroy"

# Rule Countries
post "tool-groups/:id/rule-countries", to: "rule_countries#create"
patch "tool-groups/:tool_group_id/rule-countries/:id", to: "rule_countries#update"
delete "tool-groups/:tool_group_id/rule-countries/:id", to: "rule_countries#destroy"

patch "user/counters/:id", to: "user_counters#update" # Legacy route for GodTools Android v5.7.0-v6.0.0
patch "user/me/counters/:id", to: "user_counters#update" # Legacy route for GodTools Android v6.0.1+
get "users/:user_id/counters", to: "user_counters#index"
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20230714185946_create_rule_countries.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateRuleCountries < ActiveRecord::Migration[6.1]
def change
create_table :rule_countries do |t|
t.references :tool_group, null: false, foreign_key: true
t.string :countries, array: true, default: []
t.boolean :negative_rule, default: false

t.timestamps
end
end
end
12 changes: 11 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions spec/acceptance/rule_countries_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# frozen_string_literal: true

require "acceptance_helper"

resource "RuleCountries" do
header "Accept", "application/vnd.api+json"
header "Content-Type", "application/vnd.api+json"

let(:raw_post) { params.to_json }
let(:authorization) { AuthToken.generic_token }

before(:each) do
%i[one].each do |name|
FactoryBot.create(:tool_group, name: name)
end
FactoryBot.create(:rule_country, tool_group: ToolGroup.first)
end

after(:each) do
RuleCountry.delete_all
ToolGroup.delete_all
end

post "tool-groups/:id/rule-countries" do
requires_authorization

let(:attrs) do
{
countries: ["CA", "FR", "US"],
tool_group_id: ToolGroup.first.id,
negative_rule: "true"
}
end

it "create rule country" do
do_request data: {type: "tool-group-rule-countries", attributes: attrs}

expect(status).to eq(201)
expect(JSON.parse(response_body)["data"]).not_to be_nil
end
end

patch "tool-groups/:tool_group_id/rule-countries/:id" do
requires_authorization

let(:tool_group_id) { ToolGroup.first.id }
let(:id) { RuleCountry.first.id }
let(:countries) { ["FR", "AR"] }

let(:attrs) do
{
countries: countries,
negative_rule: false
}
end

it "update rule country" do
do_request data: {type: "tool-group-rule-countries", attributes: attrs}

expect(status).to be(202)
expect(JSON.parse(response_body)["data"]["attributes"]["countries"]).to eql countries
expect(JSON.parse(response_body)["data"]["attributes"]["negative-rule"]).to eql false
end
end

delete "tool-groups/:tool_group_id/rule-countries/:id" do
requires_authorization

let(:tool_group_id) { ToolGroup.first.id }
let(:id) { RuleCountry.first.id }

it "delete rule country" do
do_request
expect(status).to be(204)
end
end
end
6 changes: 3 additions & 3 deletions spec/acceptance/rule_languages_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
}
end

it "create tool group" do
it "create rule language" do
do_request data: {type: "tool-group-rule-languages", attributes: attrs}
expect(status).to eq(201)
expect(JSON.parse(response_body)["data"]).not_to be_nil
Expand All @@ -53,7 +53,7 @@
}
end

it "update tool group" do
it "update rule language" do
do_request data: {type: "tool-group-rule-languages", attributes: attrs}

expect(status).to be(202)
Expand All @@ -68,7 +68,7 @@
let(:tool_group_id) { ToolGroup.first.id }
let(:id) { RuleLanguage.first.id }

it "delete tool_group rule language" do
it "delete rule language" do
do_request
expect(status).to be(204)
end
Expand Down
7 changes: 7 additions & 0 deletions spec/factories/rule_countries.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryBot.define do
factory :rule_country do
tool_group_id { 1 }
negative_rule { true }
countries { ["BR"] }
end
end

0 comments on commit 5e5f3af

Please sign in to comment.