Skip to content

Commit

Permalink
added extra functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kjgarza committed Aug 8, 2017
1 parent c35249e commit 83e6143
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 12 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ gem 'api-pagination'
# gem 'jsonapi-utils', '~> 0.4.9'
gem 'mysql2'
gem 'cancancan', '~> 1.10'
gem 'countries'
gem "facets", require: false

group :development, :test do
Expand Down
17 changes: 11 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ GEM
coderay (1.1.1)
colorize (0.8.1)
concurrent-ruby (1.0.5)
countries (2.1.2)
i18n_data (~> 0.8.0)
money (~> 6.9)
sixarm_ruby_unaccent (~> 1.1)
unicode_utils (~> 1.4)
crack (0.4.3)
safe_yaml (~> 1.0.0)
dalli (2.7.6)
Expand Down Expand Up @@ -136,16 +141,13 @@ GEM
hetchy (1.0.0)
htmlentities (4.3.4)
i18n (0.8.4)
i18n_data (0.8.0)
iso8601 (0.9.1)
json (1.8.6)
json-ld (2.1.4)
multi_json (~> 1.12)
rdf (~> 2.2)
jsonapi-renderer (0.1.2)
jsonapi-resources (0.9.0)
activerecord (>= 4.1)
concurrent-ruby
railties (>= 4.1)
jsonlint (0.2.0)
oj (~> 2)
trollop (~> 2)
Expand Down Expand Up @@ -193,6 +195,8 @@ GEM
mime-types-data (3.2016.0521)
mini_portile2 (2.1.0)
minitest (5.10.2)
money (6.9.0)
i18n (>= 0.6.4, < 0.9)
multi_json (1.12.1)
multipart-post (2.0.0)
mysql2 (0.4.6)
Expand Down Expand Up @@ -285,6 +289,7 @@ GEM
json (>= 1.8, < 3)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.1)
sixarm_ruby_unaccent (1.1.2)
spring (2.0.2)
activesupport (>= 4.2)
spring-watcher-listen (2.0.1)
Expand Down Expand Up @@ -335,6 +340,7 @@ DEPENDENCIES
cancancan (~> 1.10)
capybara
codeclimate-test-reporter (~> 1.0.0)
countries
dalli (~> 2.7.6)
database_cleaner
dotenv
Expand All @@ -345,7 +351,6 @@ DEPENDENCIES
gender_detector (~> 0.1.2)
iso8601 (~> 0.9.0)
json (~> 1.8, >= 1.8.5)
jsonapi-resources
jwt
librato-rails (~> 1.4.2)
listen (>= 3.0.5, < 3.2)
Expand All @@ -370,4 +375,4 @@ DEPENDENCIES
will_paginate

BUNDLED WITH
1.15.1
1.15.3
3 changes: 0 additions & 3 deletions app/controllers/members_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ def set_member
def member_params

params[:data][:attributes] = params[:data][:attributes].transform_keys!{ |key| key.to_s.snakecase }
# if params[:data][:attributes][:password]
# params[:data][:attributes][:password] = Digest::SHA256.hexdigest params[:data][:attributes][:password] + "{" + ENV["SESSION_ENCRYPTED_COOKIE_SALT"] + "}"
# end

mb_params= params[:data].require(:attributes).permit(:comments, :contact_email, :contact_name, :description, :member_type, :year, :image, :region, :country_code, :website, :logo, :doi_quota_allowed, :doi_quota_used, :is_active, :name, :password, :role_name, :member_id, :version, :experiments)
mb_params[:symbol] = mb_params[:member_id]
Expand Down
5 changes: 5 additions & 0 deletions app/models/datacenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Datacenter < ApplicationRecord
belongs_to :member, class_name: 'Member', foreign_key: :allocator
has_many :datasets

after_create :add_test_prefix


# * Increase used quota counter for a datacentre.
# *
Expand Down Expand Up @@ -84,6 +86,9 @@ def self.get_all(options={})
@datacenters
end

def add_test_prefix


end

end
11 changes: 10 additions & 1 deletion app/models/dataset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Dataset < ApplicationRecord
validates_uniqueness_of :doi, message: "This DOI has already been taken"
validates_numericality_of :version, if: :version?

before_create :is_quota_exceeded
after_create :decrease_doi_quota

def self.get_all(options={})

Expand All @@ -37,7 +39,14 @@ def self.get_all(options={})
@datasets
end

def is_quota_exceeded
datacenter = Datacenter.find(self.datacenter_id)
fail("You have excceded your DOI quota. You cannot mint DOIs anymore.") if datacenter[:doi_quota_allowed] < 0
end


def decrease_doi_quota
datacenter = Datacenter.find(self.datacenter_id)
fail("Something went wrong when decreasing your DOI quota") unless Datacenter.update(datacenter[:id], doi_quota_allowed: datacenter[:doi_quota_allowed] - 1)
end

end
9 changes: 7 additions & 2 deletions app/models/member.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Member < ApplicationRecord
require "countries"
self.table_name = "allocator"
# alias_attribute :created_at, :created
# alias_attribute :updated_at, :updated
Expand All @@ -7,14 +8,18 @@ class Member < ApplicationRecord
has_many :datacenters
validates_presence_of :name
validates_uniqueness_of :member_id, message: "This member_id has already been taken"
validates_format_of :contact_email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
validates_format_of :website, :with => /https?:\/\/[\S]+/ , if: :website?
validates_format_of :contact_email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, message: "contact_email should be an email"
validates_format_of :website, :with => /https?:\/\/[\S]+/ , if: :website?, message: "Website should be an url"
validates_format_of :logo, :with => /https?:\/\/[\S]+/ , if: :logo?, message: "Logo should be an url"
validates_numericality_of :version, if: :version?
validates_numericality_of :doi_quota_allowed, :version
validates_inclusion_of :role_name, :in => %w( ROLE_ALLOCATOR ROLE_ADMIN ROLE_DEV ), :message => "Role {{value}} is not included in the list", if: :role_name?
validates_inclusion_of :country_code, :in => ISO3166::Country.all.map(&:alpha2), :message => "must be a 2 character country representation (ISO 3166-1)", if: :country_code?

has_and_belongs_to_many :prefixes, class_name: 'Prefix', join_table: "allocator_prefixes", foreign_key: :prefixes, association_foreign_key: :allocator

after_create :add_test_prefix

def member_type
return "allocating" if doi_quota_allowed >= 0
"non_allocating"
Expand Down
5 changes: 5 additions & 0 deletions spec/requests/datacenters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@
it 'returns status code 201' do
expect(response).to have_http_status(201)
end

it 'Associates test Prefix' do
expect(json['relationships']['prefixes']['data']).to include(:id => "10.5072")
end

end

context 'when the request is invalid' do
Expand Down
10 changes: 10 additions & 0 deletions spec/requests/datasets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
describe 'POST /datasets' do
# valid payload
let!(:datacenter) { create(:datacenter) }
let!(:doi_quota_used) { datacenter.doi_quota_used }
let(:valid_attributes) { ActiveModelSerializers::Adapter.create(DatasetSerializer.new(FactoryGirl.build(:dataset, datacenter: datacenter)), {adapter: "json_api"}).to_json }

context 'when the request is valid' do
Expand All @@ -63,6 +64,10 @@
expect(json['data']['attributes']['doi']).to eq(JSON.parse(valid_attributes)['data']['attributes']['doi'])
end

it 'Increase Quota' do
expect(doi_quota_used).to lt(datacenter.doi_quota_used)
end

it 'returns status code 201' do
expect(response).to have_http_status(201)
end
Expand All @@ -75,6 +80,11 @@
it 'returns status code 500' do
expect(response).to have_http_status(500)
end

it 'doesn not Increase Quota' do
expect(doi_quota_used).to eq(datacenter.doi_quota_used)
end

# it 'returns status code 422' do
# expect(response).to have_http_status(422)
# end
Expand Down
4 changes: 4 additions & 0 deletions spec/requests/members_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
it 'returns status code 201' do
expect(response).to have_http_status(201)
end

it 'Associates test Prefix' do
expect(json['relationships']['prefixes']['data']).to include(:id => "10.5072")
end
end

context 'when the request is invalid' do
Expand Down

0 comments on commit 83e6143

Please sign in to comment.