diff --git a/app/models/item_group.rb b/app/models/item_group.rb new file mode 100644 index 00000000..5b1840dc --- /dev/null +++ b/app/models/item_group.rb @@ -0,0 +1,6 @@ +class ItemGroup < ApplicationRecord + belongs_to :item, polymorphic: true + belongs_to :root_item, polymorphic: true + + enum group_type: [:similar, :identical] +end diff --git a/app/models/user.rb b/app/models/user.rb index 0848f17a..43176394 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,7 +1,7 @@ class User < ApplicationRecord - belongs_to :group + belongs_to :user_group - delegate :study, to: :group + delegate :study, to: :user_group # Others available are: # :timeoutable and :omniauthable diff --git a/app/models/user_group.rb b/app/models/user_group.rb index e3d14127..7287e0b3 100644 --- a/app/models/user_group.rb +++ b/app/models/user_group.rb @@ -18,7 +18,7 @@ class UserGroup < ApplicationRecord private # Private methods - # In case more than just the study label is submitted extract the label for + # In case more than just the study label is submitted extract the label from # saving def remove_study_labelling if study.is_a? Array diff --git a/db/migrate/20170517153047_create_item_groups.rb b/db/migrate/20170517153047_create_item_groups.rb new file mode 100644 index 00000000..d2c518a8 --- /dev/null +++ b/db/migrate/20170517153047_create_item_groups.rb @@ -0,0 +1,11 @@ +class CreateItemGroups < ActiveRecord::Migration[5.0] + def change + create_table :item_groups do |t| + t.references :item, polymorphic: true + t.integer :group_type + t.references :root_item, polymorphic: true + + t.timestamps + end + end +end diff --git a/db/structure.sql b/db/structure.sql index ba660337..9288fde3 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -614,6 +614,41 @@ CREATE SEQUENCE instruments_id_seq ALTER SEQUENCE instruments_id_seq OWNED BY instruments.id; +-- +-- Name: item_groups; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE item_groups ( + id integer NOT NULL, + item_type character varying, + item_id integer, + group_type integer, + root_item_type character varying, + root_item_id integer, + created_at timestamp without time zone NOT NULL, + updated_at timestamp without time zone NOT NULL +); + + +-- +-- Name: item_groups_id_seq; Type: SEQUENCE; Schema: public; Owner: - +-- + +CREATE SEQUENCE item_groups_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: item_groups_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - +-- + +ALTER SEQUENCE item_groups_id_seq OWNED BY item_groups.id; + + -- -- Name: links; Type: TABLE; Schema: public; Owner: - -- @@ -1221,6 +1256,13 @@ ALTER TABLE ONLY instruments ALTER COLUMN id SET DEFAULT nextval('instruments_id ALTER TABLE ONLY instruments_datasets ALTER COLUMN id SET DEFAULT nextval('instruments_datasets_id_seq'::regclass); +-- +-- Name: id; Type: DEFAULT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY item_groups ALTER COLUMN id SET DEFAULT nextval('item_groups_id_seq'::regclass); + + -- -- Name: id; Type: DEFAULT; Schema: public; Owner: - -- @@ -1495,6 +1537,14 @@ ALTER TABLE ONLY instruments ADD CONSTRAINT instruments_pkey PRIMARY KEY (id); +-- +-- Name: item_groups_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY item_groups + ADD CONSTRAINT item_groups_pkey PRIMARY KEY (id); + + -- -- Name: links_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- @@ -1804,6 +1854,20 @@ CREATE INDEX index_instruments_datasets_on_dataset_id ON instruments_datasets US CREATE INDEX index_instruments_datasets_on_instrument_id ON instruments_datasets USING btree (instrument_id); +-- +-- Name: index_item_groups_on_item_type_and_item_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_item_groups_on_item_type_and_item_id ON item_groups USING btree (item_type, item_id); + + +-- +-- Name: index_item_groups_on_root_item_type_and_root_item_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_item_groups_on_root_item_type_and_root_item_id ON item_groups USING btree (root_item_type, root_item_id); + + -- -- Name: index_links_on_target_type_and_target_id; Type: INDEX; Schema: public; Owner: - -- @@ -2332,6 +2396,7 @@ INSERT INTO schema_migrations (version) VALUES ('20170302132603'), ('20170302132849'), ('20170505135010'), -('20170517105644'); +('20170517105644'), +('20170517153047'); diff --git a/test/fixtures/item_groups.yml b/test/fixtures/item_groups.yml new file mode 100644 index 00000000..ccf86aef --- /dev/null +++ b/test/fixtures/item_groups.yml @@ -0,0 +1,15 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + item: one + item_type: Item + group_type: MyString + root_item: one + root_item_type: Root item + +two: + item: two + item_type: Item + group_type: MyString + root_item: two + root_item_type: Root item diff --git a/test/models/item_group_test.rb b/test/models/item_group_test.rb new file mode 100644 index 00000000..a6222987 --- /dev/null +++ b/test/models/item_group_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ItemGroupTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end