From 0654f36fdf7c817a81b8b9b46c26fd9da21a871f Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Fri, 24 May 2019 11:24:25 +0200 Subject: [PATCH] use member_type instead of role_name in api. datacite/datacite#725 --- app/controllers/providers_controller.rb | 6 +++--- app/models/provider.rb | 15 ++++++--------- app/models/user.rb | 10 +++++----- db/schema.rb | 6 +++--- spec/requests/providers_spec.rb | 13 +++---------- 5 files changed, 20 insertions(+), 30 deletions(-) diff --git a/app/controllers/providers_controller.rb b/app/controllers/providers_controller.rb index 030fb6f70..c4b09cac8 100644 --- a/app/controllers/providers_controller.rb +++ b/app/controllers/providers_controller.rb @@ -108,7 +108,7 @@ def index secondaryBillingContactFamilyName twitter ror_id - role_name + member_type joined created updated @@ -236,7 +236,7 @@ def safe_params ActiveModelSerializers::Deserialization.jsonapi_parse!( params, only: [ - :name, :symbol, :description, :website, :joined, "organizationType", "focusArea", :phone, "contactName", "contactEmail", "isActive", "passwordInput", :country, "billingInformation",{ "billingInformation": ["postCode", :state, :city, :address, :department, :organization, :country]}, "rorId", "twitterHandle","roleName", + :name, :symbol, :description, :website, :joined, "organizationType", "focusArea", :phone, "contactName", "contactEmail", "isActive", "passwordInput", :country, "billingInformation",{ "billingInformation": ["postCode", :state, :city, :address, :department, :organization, :country]}, "rorId", "twitterHandle","memberType", "technicalContact",{ "technicalContact": [:email, "givenName", "familyName"]}, "secondaryBillingContact",{ "secondaryBillingContact": [:email, "givenName", "familyName"]}, "billingContact",{ "billingContact": [:email, "givenName", "familyName"]}, @@ -244,7 +244,7 @@ def safe_params "votingContact",{ "votingContact": [:email, "givenName", "familyName"]} ], keys: { - "organizationType" => :organization_type, "focusArea" => :focus_area, "contactName" => :contact_name, "contactEmail" => :contact_email, :country => :country_code, "isActive" => :is_active, "passwordInput" => :password_input, "billingInformation" => :billing_information , "postCode" => :post_code, "rorId" => :ror_id, "twitterHandle" => :twitter_handle, "roleName" => :role_name, + "organizationType" => :organization_type, "focusArea" => :focus_area, "contactName" => :contact_name, "contactEmail" => :contact_email, :country => :country_code, "isActive" => :is_active, "passwordInput" => :password_input, "billingInformation" => :billing_information , "postCode" => :post_code, "rorId" => :ror_id, "twitterHandle" => :twitter_handle, "memberType" => :member_type, "technicalContact" => :technical_contact, "secondaryBillingContact" => :secondary_billing_contact, "billingContact" => :billing_contact, diff --git a/app/models/provider.rb b/app/models/provider.rb index 862a3606d..99e61aff8 100644 --- a/app/models/provider.rb +++ b/app/models/provider.rb @@ -41,7 +41,7 @@ class Provider < ActiveRecord::Base validate :freeze_symbol, :on => :update validates_format_of :ror_id, :with => /\A(?:(http|https):\/\/)?(?:ror\.org\/)?(0\w{6}\d{2})\z/, if: :ror_id? validates_format_of :twitter_handle, :with => /\A@[a-zA-Z0-9_]{1,15}\z/, if: :twitter_handle? - + # validates :technical_contact, contact: true # validates :billing_contact, contact: true # validates :secondary_billing_contact, contact: true @@ -379,6 +379,11 @@ def member_type member_types[role_name] end + def member_type=(value) + role_name = member_types.invert.fetch(value, nil) + write_attribute(:role_name, role_name) if role_name.present? + end + def member_types { "ROLE_MEMBER" => "member_only", @@ -435,14 +440,6 @@ def password_input=(value) write_attribute(:password, encrypt_password_sha256(value)) if value.present? end - # def member_type - # if role_name == "ROLE_ALLOCATOR" - # "provider" - # elsif role_name == "ROLE_MEMBER" - # "member_only" - # end - # end - def client_ids clients.where(deleted_at: nil).pluck(:symbol).map(&:downcase) end diff --git a/app/models/user.rb b/app/models/user.rb index 22b95799d..4375cca5f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -62,25 +62,25 @@ def is_beta_tester? def provider return nil unless provider_id.present? - Provider.where(symbol: provider_id).first + Provider.where(symbol: provider_id).where(deleted_at: nil).first end def client return nil unless client_id.present? - ::Client.where(symbol: client_id).first + ::Client.where(symbol: client_id).where(deleted_at: nil).first end def self.reset(username) uid = username.downcase if uid.include?(".") - user = Client.where(symbol: uid.upcase).first + user = Client.where(symbol: uid.upcase).where(deleted_at: nil).first client_id = uid elsif uid == "admin" - user = Provider.unscoped.where(symbol: uid.upcase).first - else user = Provider.where(symbol: uid.upcase).first + else + user = Provider.where(symbol: uid.upcase).where(deleted_at: nil).first provider_id = uid end diff --git a/db/schema.rb b/db/schema.rb index b789ac3d4..077b241cb 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -12,7 +12,7 @@ ActiveRecord::Schema.define(version: 2019_05_22_162430) do - create_table "active_storage_attachments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t| + create_table "active_storage_attachments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", force: :cascade do |t| t.string "name", limit: 191, null: false t.string "record_type", null: false t.bigint "record_id", null: false @@ -22,7 +22,7 @@ t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true end - create_table "active_storage_blobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t| + create_table "active_storage_blobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", force: :cascade do |t| t.string "key", limit: 191, null: false t.string "filename", limit: 191, null: false t.string "content_type", limit: 191 @@ -80,7 +80,7 @@ t.index ["prefixes"], name: "FKE7FBD674AF86A1C7" end - create_table "audits", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t| + create_table "audits", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", force: :cascade do |t| t.integer "auditable_id" t.string "auditable_type" t.integer "associated_id" diff --git a/spec/requests/providers_spec.rb b/spec/requests/providers_spec.rb index 1497e1ca1..6633a5fc6 100644 --- a/spec/requests/providers_spec.rb +++ b/spec/requests/providers_spec.rb @@ -139,7 +139,7 @@ end end - context 'create provider type ROLE_CONTRACTUAL_PROVIDER ' do + context 'create provider member_role contractual_provider' do let(:params) do { "data" => { "type" => "providers", "attributes" => { @@ -148,7 +148,7 @@ "region" => "EMEA", "contactEmail" => "doe@joe.joe", "contactName" => "timAus", - "roleName" => "ROLE_CONTRACTUAL_PROVIDER", + "memberType" => "contractual_provider", "country" => "GB" } } } end @@ -157,16 +157,9 @@ end it 'creates a provider' do - puts json expect(json.dig('data', 'attributes', 'contactEmail')).to eq("doe@joe.joe") expect(json.dig('data', 'attributes', 'name')).to eq("Figshare") - end - - it 'provider model get computed' do - report = Provider.where(symbol: json.dig('data', 'attributes','symbol')).first - expect(report.role_name).to eq("ROLE_CONTRACTUAL_PROVIDER") - expect(report.member_type).to eq("contractual_provider") - expect(report.member_type_label).to eq("Contractual Provider") + expect(json.dig('data', 'attributes', 'memberType')).to eq("contractual_provider") end it 'returns status code 201' do