Merge pull request #1475 from FarmBot/final_groups_stuff
Add `sort_type` (API-side only)pull/1483/head
commit
2e79d97e5e
|
@ -1,4 +1,11 @@
|
|||
class PointGroup < ApplicationRecord
|
||||
SORT_TYPES =
|
||||
%w(xy_ascending xy_decending yx_ascending yx_decending random).sort
|
||||
BAD_SORT = "%{value} is not valid. Valid options are: " +
|
||||
SORT_TYPES.map(&:inspect).join(", ")
|
||||
|
||||
belongs_to :device
|
||||
has_many :point_group_items, dependent: :destroy
|
||||
validates_inclusion_of :sort_type, in: SORT_TYPES,
|
||||
message: BAD_SORT
|
||||
end
|
||||
|
|
|
@ -8,8 +8,13 @@ module PointGroups
|
|||
array :point_ids, class: Integer
|
||||
end
|
||||
|
||||
optional do
|
||||
string :sort_type
|
||||
end
|
||||
|
||||
def validate
|
||||
validate_point_ids
|
||||
validate_sort_type
|
||||
end
|
||||
|
||||
def execute
|
||||
|
|
|
@ -11,5 +11,21 @@ module PointGroups
|
|||
add_error :points, :points_bad, BAD_POINT_IDS
|
||||
end
|
||||
end
|
||||
|
||||
def validate_sort_type
|
||||
if sort_type
|
||||
bad_sort_type! unless valid_point_type?
|
||||
end
|
||||
end
|
||||
|
||||
def bad_sort_type!
|
||||
add_error :sort_type,
|
||||
:sort_type_bad,
|
||||
PointGroup::BAD_SORT % { value: sort_type }
|
||||
end
|
||||
|
||||
def valid_point_type?
|
||||
PointGroup::SORT_TYPES.include?(sort_type)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -10,10 +10,12 @@ module PointGroups
|
|||
optional do
|
||||
string :name
|
||||
array :point_ids, class: Integer
|
||||
string :sort_type
|
||||
end
|
||||
|
||||
def validate
|
||||
validate_point_ids if point_ids.any?
|
||||
validate_sort_type
|
||||
end
|
||||
|
||||
def execute
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
class PointGroupSerializer < ApplicationSerializer
|
||||
attributes :name, :point_ids
|
||||
attributes :name, :point_ids, :sort_type
|
||||
|
||||
def point_ids
|
||||
object.point_group_items.pluck(:point_id)
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
class AddSortOptionsToPointGroups < ActiveRecord::Migration[5.2]
|
||||
safety_assured
|
||||
|
||||
def change
|
||||
add_column :point_groups,
|
||||
:sort_type,
|
||||
:string,
|
||||
limit: 20,
|
||||
default: "xy_ascending"
|
||||
end
|
||||
end
|
|
@ -1149,7 +1149,8 @@ CREATE TABLE public.point_groups (
|
|||
name character varying(80) NOT NULL,
|
||||
device_id bigint NOT NULL,
|
||||
created_at timestamp without time zone NOT NULL,
|
||||
updated_at timestamp without time zone NOT NULL
|
||||
updated_at timestamp without time zone NOT NULL,
|
||||
sort_type character varying(20) DEFAULT 'xy_ascending'::character varying
|
||||
);
|
||||
|
||||
|
||||
|
@ -3275,6 +3276,7 @@ INSERT INTO "schema_migrations" (version) VALUES
|
|||
('20190804194154'),
|
||||
('20190823164837'),
|
||||
('20190918185359'),
|
||||
('20190924190539');
|
||||
('20190924190539'),
|
||||
('20190930202839');
|
||||
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@ describe Api::PointGroupsController do
|
|||
point_ids.map do |this_id|
|
||||
expect(json[:point_ids]).to include(this_id)
|
||||
end
|
||||
expect(json[:sort_type]).to eq("xy_ascending")
|
||||
end
|
||||
|
||||
it "alerts the user about bad point_ids" do
|
||||
|
@ -37,4 +38,13 @@ describe Api::PointGroupsController do
|
|||
expect(before).to eq(PointGroup.count)
|
||||
expect(json.fetch(:points)).to include(PointGroups::Create::BAD_POINT_IDS)
|
||||
end
|
||||
|
||||
it "disallows malformed sort_types" do
|
||||
sign_in user
|
||||
payload = { name: "_", point_ids: [], sort_type: "q" }
|
||||
before = PointGroup.count
|
||||
post :create, body: payload.to_json, format: :json
|
||||
expect(response.status).to eq(422)
|
||||
expect(json[:sort_type]).to include(PointGroup::BAD_SORT.split("}").last)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue