Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions app/controllers/admin/suggested_patterns_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

module Admin
class SuggestedPatternsController < Admin::ApplicationController
# Overwrite any of the RESTful controller actions to implement custom behavior
# For example, you may want to send an email after a foo is updated.
#
# def update
# super
# send_foo_updated_email(requested_resource)
# end

# Override this method to specify custom lookup behavior.
# This will be used to set the resource for the `show`, `edit`, and `update`
# actions.
#
# def find_resource(param)
# Foo.find_by!(slug: param)
# end

# The result of this lookup will be available as `requested_resource`

# Override this if you have certain roles that require a subset
# this will be used to set the records shown on the `index` action.
#
# def scoped_resource
# if current_user.super_admin?
# resource_class
# else
# resource_class.with_less_stuff
# end
# end

# Override `resource_params` if you want to transform the submitted
# data before it's persisted. For example, the following would turn all
# empty values into nil values. It uses other APIs such as `resource_class`
# and `dashboard`:
#
# def resource_params
# params.require(resource_class.model_name.param_key).
# permit(dashboard.permitted_attributes(action_name)).
# transform_values { |value| value == "" ? nil : value }
# end

# See https://administrate-demo.herokuapp.com/customizing_controller_actions
# for more information
end
end
48 changes: 48 additions & 0 deletions app/controllers/admin/suggested_resources_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

module Admin
class SuggestedResourcesController < Admin::ApplicationController
# Overwrite any of the RESTful controller actions to implement custom behavior
# For example, you may want to send an email after a foo is updated.
#
# def update
# super
# send_foo_updated_email(requested_resource)
# end

# Override this method to specify custom lookup behavior.
# This will be used to set the resource for the `show`, `edit`, and `update`
# actions.
#
# def find_resource(param)
# Foo.find_by!(slug: param)
# end

# The result of this lookup will be available as `requested_resource`

# Override this if you have certain roles that require a subset
# this will be used to set the records shown on the `index` action.
#
# def scoped_resource
# if current_user.super_admin?
# resource_class
# else
# resource_class.with_less_stuff
# end
# end

# Override `resource_params` if you want to transform the submitted
# data before it's persisted. For example, the following would turn all
# empty values into nil values. It uses other APIs such as `resource_class`
# and `dashboard`:
#
# def resource_params
# params.require(resource_class.model_name.param_key).
# permit(dashboard.permitted_attributes(action_name)).
# transform_values { |value| value == "" ? nil : value }
# end

# See https://administrate-demo.herokuapp.com/customizing_controller_actions
# for more information
end
end
80 changes: 80 additions & 0 deletions app/dashboards/suggested_pattern_dashboard.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# frozen_string_literal: true

require 'administrate/base_dashboard'

class SuggestedPatternDashboard < Administrate::BaseDashboard
# ATTRIBUTE_TYPES
# a hash that describes the type of each of the model's fields.
#
# Each different type represents an Administrate::Field object,
# which determines how the attribute is displayed
# on pages throughout the dashboard.
ATTRIBUTE_TYPES = {
id: Field::Number,
category: Field::BelongsTo,
confidence: Field::Number.with_options(decimals: 2),
pattern: Field::String,
shortcode: Field::String,
title: Field::String,
url: Field::String,
created_at: Field::DateTime,
updated_at: Field::DateTime
}.freeze

# COLLECTION_ATTRIBUTES
# an array of attributes that will be displayed on the model's index page.
#
# By default, it's limited to four items to reduce clutter on index pages.
# Feel free to add, remove, or rearrange items.
COLLECTION_ATTRIBUTES = %i[
id
category
pattern
shortcode
].freeze

# SHOW_PAGE_ATTRIBUTES
# an array of attributes that will be displayed on the model's show page.
SHOW_PAGE_ATTRIBUTES = %i[
id
category
confidence
pattern
shortcode
title
url
created_at
updated_at
].freeze

# FORM_ATTRIBUTES
# an array of attributes that will be displayed
# on the model's form (`new` and `edit`) pages.
FORM_ATTRIBUTES = %i[
category
confidence
pattern
shortcode
title
url
].freeze

# COLLECTION_FILTERS
# a hash that defines filters that can be used while searching via the search
# field of the dashboard.
#
# For example to add an option to search for open resources by typing "open:"
# in the search field:
#
# COLLECTION_FILTERS = {
# open: ->(resources) { resources.where(open: true) }
# }.freeze
COLLECTION_FILTERS = {}.freeze

# Overwrite this method to customize how suggested patterns are displayed
# across all pages of the admin dashboard.
#
# def display_resource(suggested_pattern)
# "SuggestedPattern ##{suggested_pattern.id}"
# end
end
78 changes: 78 additions & 0 deletions app/dashboards/suggested_resource_dashboard.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# frozen_string_literal: true

require 'administrate/base_dashboard'

class SuggestedResourceDashboard < Administrate::BaseDashboard
# ATTRIBUTE_TYPES
# a hash that describes the type of each of the model's fields.
#
# Each different type represents an Administrate::Field object,
# which determines how the attribute is displayed
# on pages throughout the dashboard.
ATTRIBUTE_TYPES = {
id: Field::Number,
category: Field::BelongsTo,
confidence: Field::Number.with_options(decimals: 2),
fingerprints: Field::HasMany,
terms: Field::HasMany,
title: Field::String,
url: Field::String,
created_at: Field::DateTime,
updated_at: Field::DateTime
}.freeze

# COLLECTION_ATTRIBUTES
# an array of attributes that will be displayed on the model's index page.
#
# By default, it's limited to four items to reduce clutter on index pages.
# Feel free to add, remove, or rearrange items.
COLLECTION_ATTRIBUTES = %i[
id
title
url
terms
category
].freeze

# SHOW_PAGE_ATTRIBUTES
# an array of attributes that will be displayed on the model's show page.
SHOW_PAGE_ATTRIBUTES = %i[
id
category
confidence
terms
title
url
created_at
updated_at
].freeze

# FORM_ATTRIBUTES
# an array of attributes that will be displayed
# on the model's form (`new` and `edit`) pages.
FORM_ATTRIBUTES = %i[
category
confidence
title
url
].freeze

# COLLECTION_FILTERS
# a hash that defines filters that can be used while searching via the search
# field of the dashboard.
#
# For example to add an option to search for open resources by typing "open:"
# in the search field:
#
# COLLECTION_FILTERS = {
# open: ->(resources) { resources.where(open: true) }
# }.freeze
COLLECTION_FILTERS = {}.freeze

# Overwrite this method to customize how suggested resources are displayed
# across all pages of the admin dashboard.
#
# def display_resource(suggested_resource)
# "SuggestedResource ##{suggested_resource.id}"
# end
end
13 changes: 11 additions & 2 deletions app/models/detector/suggested_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ def self.full_term_match(phrase)
# @note Multiple detections are irrelevant for this method. If _any_ match is found, a Detection record is created.
# The uniqueness contraint on Detection records would make multiple detections irrelevant.
#
# @return nil
# @return Hash with keys :category and :confidence (or nil)
def self.record(term)
result = full_term_match(term.phrase)

return unless result.any?

Detection.find_or_create_by(
Expand All @@ -38,7 +39,15 @@ def self.record(term)
detector_version: ENV.fetch('DETECTOR_VERSION', 'unset')
)

nil
return if result.empty?

# If a category hasn't been set, nil is better than a confidence with no category
return if result.first.category.blank?

{
category: result.first.category,
confidence: result.first.confidence
}
end
end
end
15 changes: 12 additions & 3 deletions app/models/detector/suggested_resource_pattern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ def check_patterns(phrase)
sps << {
shortcode: sp.shortcode,
title: sp.title,
url: sp.url
url: sp.url,
category: sp.category,
confidence: sp.confidence
}
@detections = sps
end
Expand All @@ -39,7 +41,7 @@ def check_patterns(phrase)
# @note There are multiple patterns within SuggestedPattern records. Each check is capable of generating
# a separate Detection record.
#
# @return nil
# @return Hash with keys :category and :confidence (or nil)
def self.record(term)
sp = Detector::SuggestedResourcePattern.new(term.phrase)

Expand All @@ -50,8 +52,15 @@ def self.record(term)
detector_version: ENV.fetch('DETECTOR_VERSION', 'unset')
)
end
return if sp.detections.empty?

nil
# If a category hasn't been set, nil is better than a confidence with no category
return if sp.detections.first[:category].blank?

{
category: sp.detections.first[:category],
confidence: sp.detections.first[:confidence]
}
end
end
end
18 changes: 11 additions & 7 deletions app/models/suggested_pattern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
#
# Table name: suggested_patterns
#
# id :integer not null, primary key
# title :string not null
# url :string not null
# pattern :string not null
# shortcode :string not null
# created_at :datetime not null
# updated_at :datetime not null
# id :integer not null, primary key
# title :string not null
# url :string not null
# pattern :string not null
# shortcode :string not null
# created_at :datetime not null
# updated_at :datetime not null
# category_id :integer
# confidence :float default(0.9)
#
class SuggestedPattern < ApplicationRecord
validates :title, presence: true
validates :url, presence: true
validates :pattern, presence: true, uniqueness: true
validates :shortcode, presence: true, uniqueness: true

belongs_to :category, optional: true
end
17 changes: 9 additions & 8 deletions app/models/suggested_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
#
# Table name: suggested_resources
#
# id :integer not null, primary key
# title :string
# url :string
# created_at :datetime not null
# updated_at :datetime not null
# id :integer not null, primary key
# title :string
# url :string
# created_at :datetime not null
# updated_at :datetime not null
# category_id :integer
# confidence :float default(0.9)
#
# SuggestedResource stores custom hints that we want to send to the
# user in response to specific strings. For example, a search for "web of
# science" should be met with our custom login link to Web of Science via MIT.
class SuggestedResource < ApplicationRecord
has_many :terms, dependent: :nullify
has_many :fingerprints, through: :terms, dependent: :nullify

belongs_to :category, optional: true

# This replaces all current SuggestedResource records with a new set from an imported CSV.
#
# @note This method is called by the suggested_resource:reload rake task.
Expand Down
Loading