-
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.
- Loading branch information
Showing
10 changed files
with
163 additions
and
5 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
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 |
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,3 @@ | ||
class RuleCountry < ApplicationRecord | ||
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
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 @@ | ||
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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 |
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 @@ | ||
FactoryBot.define do | ||
factory :rule_country do | ||
tool_group_id { 1 } | ||
negative_rule { true } | ||
countries { ["BR"] } | ||
end | ||
end |