Skip to content

Commit

Permalink
index client and prefix ids in has_many associations
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed May 19, 2019
1 parent fd9476c commit 7ed7b17
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/models/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Client < ActiveRecord::Base
validate :freeze_symbol, :on => :update
strip_attributes

belongs_to :provider, foreign_key: :allocator
belongs_to :provider, foreign_key: :allocator, touch: true
has_many :dois, foreign_key: :datacentre
has_many :client_prefixes, foreign_key: :datacentre, dependent: :destroy
has_many :prefixes, through: :client_prefixes
Expand Down
2 changes: 1 addition & 1 deletion app/models/client_prefix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ClientPrefix < ActiveRecord::Base

self.table_name = "datacentre_prefixes"

belongs_to :client, foreign_key: :datacentre
belongs_to :client, foreign_key: :datacentre, touch: true
belongs_to :prefix, foreign_key: :prefixes
belongs_to :provider_prefix, foreign_key: :allocator_prefixes

Expand Down
5 changes: 5 additions & 0 deletions app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ module Indexable
end
end

after_touch do
# use index_document instead of update_document to also update virtual attributes
IndexJob.perform_later(self)
end

before_destroy do
begin
__elasticsearch__.delete_document
Expand Down
4 changes: 2 additions & 2 deletions app/models/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class Provider < ActiveRecord::Base
indexes :id, type: :keyword
indexes :uid, type: :keyword
indexes :symbol, type: :keyword
indexes :client_ids, type: :keyword

This comment has been minimized.

Copy link
@kjgarza

kjgarza May 20, 2019

Contributor
indexes :prefix_ids, type: :keyword
indexes :name, type: :text, fields: { keyword: { type: "keyword" }, raw: { type: "text", "analyzer": "string_lowercase", "fielddata": true }}
indexes :contact_name, type: :text
Expand Down Expand Up @@ -120,6 +121,7 @@ def as_indexed_json(options={})
"id" => uid,
"uid" => uid,
"name" => name,
"client_ids" => client_ids,
"prefix_ids" => prefix_ids,
"symbol" => symbol,
"year" => year,
Expand Down Expand Up @@ -172,7 +174,6 @@ def self.query_aggregations
end

def csv

provider = {
name: name,
provider_id: symbol,
Expand Down Expand Up @@ -227,7 +228,6 @@ def billing_organization
billing_information.fetch("organization",nil) if billing_information.present?
end


def billing_address
billing_information.fetch("address",nil) if billing_information.present?
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/provider_prefix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ProviderPrefix < ActiveRecord::Base

self.table_name = "allocator_prefixes"

belongs_to :provider, foreign_key: :allocator
belongs_to :provider, foreign_key: :allocator, touch: true
belongs_to :prefix, foreign_key: :prefixes
has_many :client_prefixes, foreign_key: :allocator_prefixes, dependent: :destroy
has_many :clients, through: :client_prefixes
Expand Down
1 change: 1 addition & 0 deletions app/serializers/provider_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class ProviderSerializer

attributes :name, :symbol, :website, :contact_name, :contact_email, :phone, :description, :region, :country, :logo_url, :organization_type, :focus_area, :is_active, :has_password, :joined, :twitter_handle, :billing_information, :ror_id, :created, :updated

has_many :clients, record_type: :clients
has_many :prefixes, record_type: :prefixes

attribute :country do |object|
Expand Down

0 comments on commit 7ed7b17

Please sign in to comment.