From 68f0e91259d9c706572d2622196f57b64d82c745 Mon Sep 17 00:00:00 2001 From: Connor Turland Date: Sun, 18 Dec 2016 16:17:51 -0500 Subject: [PATCH] track forks (#994) * track forks * update api and docs * fix tests --- app/controllers/maps_controller.rb | 2 +- app/models/map.rb | 1 + app/serializers/api/v2/map_serializer.rb | 1 + db/migrate/20161218183817_add_source_to_maps.rb | 5 +++++ db/schema.rb | 5 ++++- doc/api/apis/maps.raml | 12 ++++++++++-- doc/api/examples/map.json | 1 + doc/api/examples/map_starred.json | 1 + doc/api/examples/maps.json | 1 + doc/api/schemas/_map.json | 12 ++++++++++++ doc/api/schemas/_optid.json | 3 +++ frontend/src/Metamaps/GlobalUI/CreateMap.js | 1 + spec/api/v2/maps_api_spec.rb | 7 ++++--- spec/factories/maps.rb | 1 + 14 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20161218183817_add_source_to_maps.rb create mode 100644 doc/api/schemas/_optid.json diff --git a/app/controllers/maps_controller.rb b/app/controllers/maps_controller.rb index 6e1e0d77..189ae550 100644 --- a/app/controllers/maps_controller.rb +++ b/app/controllers/maps_controller.rb @@ -124,7 +124,7 @@ class MapsController < ApplicationController end def create_map_params - params.permit(:name, :desc, :permission) + params.permit(:name, :desc, :permission, :source_id) end def update_map_params diff --git a/app/models/map.rb b/app/models/map.rb index 899992c6..79b4ae35 100644 --- a/app/models/map.rb +++ b/app/models/map.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true class Map < ApplicationRecord belongs_to :user + belongs_to :source, class_name: :Map has_many :topicmappings, -> { Mapping.topicmapping }, class_name: :Mapping, dependent: :destroy has_many :synapsemappings, -> { Mapping.synapsemapping }, class_name: :Mapping, dependent: :destroy diff --git a/app/serializers/api/v2/map_serializer.rb b/app/serializers/api/v2/map_serializer.rb index ff641c69..7e090d33 100644 --- a/app/serializers/api/v2/map_serializer.rb +++ b/app/serializers/api/v2/map_serializer.rb @@ -18,6 +18,7 @@ module Api def self.embeddable { user: {}, + source: {}, topics: {}, synapses: {}, mappings: {}, diff --git a/db/migrate/20161218183817_add_source_to_maps.rb b/db/migrate/20161218183817_add_source_to_maps.rb new file mode 100644 index 00000000..d6ce5fbf --- /dev/null +++ b/db/migrate/20161218183817_add_source_to_maps.rb @@ -0,0 +1,5 @@ +class AddSourceToMaps < ActiveRecord::Migration[5.0] + def change + add_reference :maps, :source, foreign_key: {to_table: :maps} + end +end diff --git a/db/schema.rb b/db/schema.rb index b30d597e..7d146be5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20161216174257) do +ActiveRecord::Schema.define(version: 20161218183817) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -151,6 +151,8 @@ ActiveRecord::Schema.define(version: 20161216174257) do t.string "screenshot_content_type", limit: 255 t.integer "screenshot_file_size" t.datetime "screenshot_updated_at" + t.integer "source_id" + t.index ["source_id"], name: "index_maps_on_source_id", using: :btree t.index ["user_id"], name: "index_maps_on_user_id", using: :btree end @@ -340,5 +342,6 @@ ActiveRecord::Schema.define(version: 20161216174257) do add_foreign_key "mailboxer_notifications", "mailboxer_conversations", column: "conversation_id", name: "notifications_on_conversation_id" add_foreign_key "mailboxer_receipts", "mailboxer_notifications", column: "notification_id", name: "receipts_on_notification_id" add_foreign_key "mappings", "users", column: "updated_by_id" + add_foreign_key "maps", "maps", column: "source_id" add_foreign_key "tokens", "users" end diff --git a/doc/api/apis/maps.raml b/doc/api/apis/maps.raml index d3361a70..fe9fed67 100644 --- a/doc/api/apis/maps.raml +++ b/doc/api/apis/maps.raml @@ -1,6 +1,6 @@ #type: collection get: - is: [ searchable: { searchFields: "name, desc" }, embeddable: { embedFields: "user,topics,synapses,mappings,contributors,collaborators" }, orderable, pageable ] + is: [ searchable: { searchFields: "name, desc" }, embeddable: { embedFields: "user,source,topics,synapses,mappings,contributors,collaborators" }, orderable, pageable ] securedBy: [ null, token, oauth_2_0, cookie ] queryParameters: user_id: @@ -23,6 +23,8 @@ post: description: description permission: description: commons, public, or private + source_id: + description: the id of the map this map is a fork of screenshot: description: url to a screenshot of the map contributor_ids: @@ -37,7 +39,7 @@ post: /{id}: #type: item get: - is: [ embeddable: { embedFields: "user,topics,synapses,mappings,contributors,collaborators" } ] + is: [ embeddable: { embedFields: "user,source,topics,synapses,mappings,contributors,collaborators" } ] securedBy: [ null, token, oauth_2_0, cookie ] responses: 200: @@ -60,6 +62,9 @@ post: screenshot: description: url to a screenshot of the map required: false + source_id: + description: the id of the map this map is a fork of + required: false responses: 200: body: @@ -81,6 +86,9 @@ post: screenshot: description: url to a screenshot of the map required: false + source_id: + description: the id of the map this map is a fork of + required: false responses: 200: body: diff --git a/doc/api/examples/map.json b/doc/api/examples/map.json index 78711649..d50d36fd 100644 --- a/doc/api/examples/map.json +++ b/doc/api/examples/map.json @@ -9,6 +9,7 @@ "created_at": "2016-03-26T08:02:05.379Z", "updated_at": "2016-03-27T07:20:18.047Z", "user_id": 1234, + "source_id": null, "topic_ids": [ 58, 59 diff --git a/doc/api/examples/map_starred.json b/doc/api/examples/map_starred.json index 4ac6c698..1fde87d6 100644 --- a/doc/api/examples/map_starred.json +++ b/doc/api/examples/map_starred.json @@ -9,6 +9,7 @@ "created_at": "2016-03-26T08:02:05.379Z", "updated_at": "2016-03-27T07:20:18.047Z", "user_id": 1234, + "source_id": null, "topic_ids": [ 58, 59 diff --git a/doc/api/examples/maps.json b/doc/api/examples/maps.json index 1e2a7baf..687f8cc5 100644 --- a/doc/api/examples/maps.json +++ b/doc/api/examples/maps.json @@ -10,6 +10,7 @@ "created_at": "2016-03-26T08:02:05.379Z", "updated_at": "2016-03-27T07:20:18.047Z", "user_id": 1234, + "source_id": 2, "topic_ids": [ 58, 59 diff --git a/doc/api/schemas/_map.json b/doc/api/schemas/_map.json index d8d9c138..7e2381f8 100644 --- a/doc/api/schemas/_map.json +++ b/doc/api/schemas/_map.json @@ -33,6 +33,12 @@ "user": { "$ref": "_user.json" }, + "source_id": { + "$ref": "_optid.json" + }, + "source": { + "$ref": "_map.json" + }, "topic_ids": { "type": "array", "items": { @@ -111,6 +117,12 @@ { "required": [ "user" ] } ] }, + { + "oneOf": [ + { "required": [ "source_id" ] }, + { "required": [ "source" ] } + ] + }, { "oneOf": [ { "required": [ "topic_ids" ] }, diff --git a/doc/api/schemas/_optid.json b/doc/api/schemas/_optid.json new file mode 100644 index 00000000..c34df067 --- /dev/null +++ b/doc/api/schemas/_optid.json @@ -0,0 +1,3 @@ +{ + "type": "integer|nil" +} diff --git a/frontend/src/Metamaps/GlobalUI/CreateMap.js b/frontend/src/Metamaps/GlobalUI/CreateMap.js index 9a4d8770..e7db6219 100644 --- a/frontend/src/Metamaps/GlobalUI/CreateMap.js +++ b/frontend/src/Metamaps/GlobalUI/CreateMap.js @@ -61,6 +61,7 @@ const CreateMap = { if (GlobalUI.lightbox === 'forkmap') { self.newMap.set('topicsToMap', self.topicsToMap) self.newMap.set('synapsesToMap', self.synapsesToMap) + self.newMap.set('source_id', Active.Map.id) } var formId = GlobalUI.lightbox === 'forkmap' ? '#fork_map' : '#new_map' diff --git a/spec/api/v2/maps_api_spec.rb b/spec/api/v2/maps_api_spec.rb index fbf07903..f7e30751 100644 --- a/spec/api/v2/maps_api_spec.rb +++ b/spec/api/v2/maps_api_spec.rb @@ -5,7 +5,8 @@ require 'rails_helper' RSpec.describe 'maps API', type: :request do let(:user) { create(:user, admin: true) } let(:token) { create(:token, user: user).token } - let(:map) { create(:map, user: user) } + let(:source) { create(:map, user: user) } + let(:map) { create(:map, user: user, source: source) } describe 'GET /api/v2/maps' do it 'returns all maps' do @@ -42,7 +43,7 @@ RSpec.describe 'maps API', type: :request do expect(response).to have_http_status(:success) expect(response).to match_json_schema(:map) - expect(Map.count).to eq 2 + expect(Map.count).to eq 3 end it 'PATCH /api/v2/maps/:id' do @@ -56,7 +57,7 @@ RSpec.describe 'maps API', type: :request do delete "/api/v2/maps/#{map.id}", params: { access_token: token } expect(response).to have_http_status(:no_content) - expect(Map.count).to eq 0 + expect(Map.count).to eq 1 end it 'POST /api/v2/maps/:id/stars' do diff --git a/spec/factories/maps.rb b/spec/factories/maps.rb index a95590e4..c69b43bb 100644 --- a/spec/factories/maps.rb +++ b/spec/factories/maps.rb @@ -4,6 +4,7 @@ FactoryGirl.define do sequence(:name) { |n| "Cool Map ##{n}" } permission :commons arranged { false } + source_id nil desc '' user end