Skip to content

Commit

Permalink
use member_type instead of role_name in api. datacite/datacite#725
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed May 24, 2019
1 parent 89e8b00 commit 0654f36
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 30 deletions.
6 changes: 3 additions & 3 deletions app/controllers/providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def index
secondaryBillingContactFamilyName
twitter
ror_id
role_name
member_type
joined
created
updated
Expand Down Expand Up @@ -236,15 +236,15 @@ 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"]},
"serviceContact",{ "serviceContact": [:email, "givenName", "familyName"]},
"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,
Expand Down
15 changes: 6 additions & 9 deletions app/models/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down
13 changes: 3 additions & 10 deletions spec/requests/providers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" => {
Expand All @@ -148,7 +148,7 @@
"region" => "EMEA",
"contactEmail" => "[email protected]",
"contactName" => "timAus",
"roleName" => "ROLE_CONTRACTUAL_PROVIDER",
"memberType" => "contractual_provider",
"country" => "GB" } } }
end

Expand All @@ -157,16 +157,9 @@
end

it 'creates a provider' do
puts json
expect(json.dig('data', 'attributes', 'contactEmail')).to eq("[email protected]")
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
Expand Down

0 comments on commit 0654f36

Please sign in to comment.