Skip to content

Commit abe9015

Browse files
authored
Merge pull request #213 from MITLibraries/record-level-categorizations
Adds categories to SuggestedResources and Patterns
2 parents b1c23b0 + 9787652 commit abe9015

21 files changed

Lines changed: 512 additions & 52 deletions
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# frozen_string_literal: true
2+
3+
module Admin
4+
class SuggestedPatternsController < Admin::ApplicationController
5+
# Overwrite any of the RESTful controller actions to implement custom behavior
6+
# For example, you may want to send an email after a foo is updated.
7+
#
8+
# def update
9+
# super
10+
# send_foo_updated_email(requested_resource)
11+
# end
12+
13+
# Override this method to specify custom lookup behavior.
14+
# This will be used to set the resource for the `show`, `edit`, and `update`
15+
# actions.
16+
#
17+
# def find_resource(param)
18+
# Foo.find_by!(slug: param)
19+
# end
20+
21+
# The result of this lookup will be available as `requested_resource`
22+
23+
# Override this if you have certain roles that require a subset
24+
# this will be used to set the records shown on the `index` action.
25+
#
26+
# def scoped_resource
27+
# if current_user.super_admin?
28+
# resource_class
29+
# else
30+
# resource_class.with_less_stuff
31+
# end
32+
# end
33+
34+
# Override `resource_params` if you want to transform the submitted
35+
# data before it's persisted. For example, the following would turn all
36+
# empty values into nil values. It uses other APIs such as `resource_class`
37+
# and `dashboard`:
38+
#
39+
# def resource_params
40+
# params.require(resource_class.model_name.param_key).
41+
# permit(dashboard.permitted_attributes(action_name)).
42+
# transform_values { |value| value == "" ? nil : value }
43+
# end
44+
45+
# See https://administrate-demo.herokuapp.com/customizing_controller_actions
46+
# for more information
47+
end
48+
end
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# frozen_string_literal: true
2+
3+
module Admin
4+
class SuggestedResourcesController < Admin::ApplicationController
5+
# Overwrite any of the RESTful controller actions to implement custom behavior
6+
# For example, you may want to send an email after a foo is updated.
7+
#
8+
# def update
9+
# super
10+
# send_foo_updated_email(requested_resource)
11+
# end
12+
13+
# Override this method to specify custom lookup behavior.
14+
# This will be used to set the resource for the `show`, `edit`, and `update`
15+
# actions.
16+
#
17+
# def find_resource(param)
18+
# Foo.find_by!(slug: param)
19+
# end
20+
21+
# The result of this lookup will be available as `requested_resource`
22+
23+
# Override this if you have certain roles that require a subset
24+
# this will be used to set the records shown on the `index` action.
25+
#
26+
# def scoped_resource
27+
# if current_user.super_admin?
28+
# resource_class
29+
# else
30+
# resource_class.with_less_stuff
31+
# end
32+
# end
33+
34+
# Override `resource_params` if you want to transform the submitted
35+
# data before it's persisted. For example, the following would turn all
36+
# empty values into nil values. It uses other APIs such as `resource_class`
37+
# and `dashboard`:
38+
#
39+
# def resource_params
40+
# params.require(resource_class.model_name.param_key).
41+
# permit(dashboard.permitted_attributes(action_name)).
42+
# transform_values { |value| value == "" ? nil : value }
43+
# end
44+
45+
# See https://administrate-demo.herokuapp.com/customizing_controller_actions
46+
# for more information
47+
end
48+
end
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# frozen_string_literal: true
2+
3+
require 'administrate/base_dashboard'
4+
5+
class SuggestedPatternDashboard < Administrate::BaseDashboard
6+
# ATTRIBUTE_TYPES
7+
# a hash that describes the type of each of the model's fields.
8+
#
9+
# Each different type represents an Administrate::Field object,
10+
# which determines how the attribute is displayed
11+
# on pages throughout the dashboard.
12+
ATTRIBUTE_TYPES = {
13+
id: Field::Number,
14+
category: Field::BelongsTo,
15+
confidence: Field::Number.with_options(decimals: 2),
16+
pattern: Field::String,
17+
shortcode: Field::String,
18+
title: Field::String,
19+
url: Field::String,
20+
created_at: Field::DateTime,
21+
updated_at: Field::DateTime
22+
}.freeze
23+
24+
# COLLECTION_ATTRIBUTES
25+
# an array of attributes that will be displayed on the model's index page.
26+
#
27+
# By default, it's limited to four items to reduce clutter on index pages.
28+
# Feel free to add, remove, or rearrange items.
29+
COLLECTION_ATTRIBUTES = %i[
30+
id
31+
category
32+
pattern
33+
shortcode
34+
].freeze
35+
36+
# SHOW_PAGE_ATTRIBUTES
37+
# an array of attributes that will be displayed on the model's show page.
38+
SHOW_PAGE_ATTRIBUTES = %i[
39+
id
40+
category
41+
confidence
42+
pattern
43+
shortcode
44+
title
45+
url
46+
created_at
47+
updated_at
48+
].freeze
49+
50+
# FORM_ATTRIBUTES
51+
# an array of attributes that will be displayed
52+
# on the model's form (`new` and `edit`) pages.
53+
FORM_ATTRIBUTES = %i[
54+
category
55+
confidence
56+
pattern
57+
shortcode
58+
title
59+
url
60+
].freeze
61+
62+
# COLLECTION_FILTERS
63+
# a hash that defines filters that can be used while searching via the search
64+
# field of the dashboard.
65+
#
66+
# For example to add an option to search for open resources by typing "open:"
67+
# in the search field:
68+
#
69+
# COLLECTION_FILTERS = {
70+
# open: ->(resources) { resources.where(open: true) }
71+
# }.freeze
72+
COLLECTION_FILTERS = {}.freeze
73+
74+
# Overwrite this method to customize how suggested patterns are displayed
75+
# across all pages of the admin dashboard.
76+
#
77+
# def display_resource(suggested_pattern)
78+
# "SuggestedPattern ##{suggested_pattern.id}"
79+
# end
80+
end
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# frozen_string_literal: true
2+
3+
require 'administrate/base_dashboard'
4+
5+
class SuggestedResourceDashboard < Administrate::BaseDashboard
6+
# ATTRIBUTE_TYPES
7+
# a hash that describes the type of each of the model's fields.
8+
#
9+
# Each different type represents an Administrate::Field object,
10+
# which determines how the attribute is displayed
11+
# on pages throughout the dashboard.
12+
ATTRIBUTE_TYPES = {
13+
id: Field::Number,
14+
category: Field::BelongsTo,
15+
confidence: Field::Number.with_options(decimals: 2),
16+
fingerprints: Field::HasMany,
17+
terms: Field::HasMany,
18+
title: Field::String,
19+
url: Field::String,
20+
created_at: Field::DateTime,
21+
updated_at: Field::DateTime
22+
}.freeze
23+
24+
# COLLECTION_ATTRIBUTES
25+
# an array of attributes that will be displayed on the model's index page.
26+
#
27+
# By default, it's limited to four items to reduce clutter on index pages.
28+
# Feel free to add, remove, or rearrange items.
29+
COLLECTION_ATTRIBUTES = %i[
30+
id
31+
title
32+
url
33+
terms
34+
category
35+
].freeze
36+
37+
# SHOW_PAGE_ATTRIBUTES
38+
# an array of attributes that will be displayed on the model's show page.
39+
SHOW_PAGE_ATTRIBUTES = %i[
40+
id
41+
category
42+
confidence
43+
terms
44+
title
45+
url
46+
created_at
47+
updated_at
48+
].freeze
49+
50+
# FORM_ATTRIBUTES
51+
# an array of attributes that will be displayed
52+
# on the model's form (`new` and `edit`) pages.
53+
FORM_ATTRIBUTES = %i[
54+
category
55+
confidence
56+
title
57+
url
58+
].freeze
59+
60+
# COLLECTION_FILTERS
61+
# a hash that defines filters that can be used while searching via the search
62+
# field of the dashboard.
63+
#
64+
# For example to add an option to search for open resources by typing "open:"
65+
# in the search field:
66+
#
67+
# COLLECTION_FILTERS = {
68+
# open: ->(resources) { resources.where(open: true) }
69+
# }.freeze
70+
COLLECTION_FILTERS = {}.freeze
71+
72+
# Overwrite this method to customize how suggested resources are displayed
73+
# across all pages of the admin dashboard.
74+
#
75+
# def display_resource(suggested_resource)
76+
# "SuggestedResource ##{suggested_resource.id}"
77+
# end
78+
end

app/models/detector/suggested_resource.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ def self.full_term_match(phrase)
2727
# @note Multiple detections are irrelevant for this method. If _any_ match is found, a Detection record is created.
2828
# The uniqueness contraint on Detection records would make multiple detections irrelevant.
2929
#
30-
# @return nil
30+
# @return Hash with keys :category and :confidence (or nil)
3131
def self.record(term)
3232
result = full_term_match(term.phrase)
33+
3334
return unless result.any?
3435

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

41-
nil
42+
return if result.empty?
43+
44+
# If a category hasn't been set, nil is better than a confidence with no category
45+
return if result.first.category.blank?
46+
47+
{
48+
category: result.first.category,
49+
confidence: result.first.confidence
50+
}
4251
end
4352
end
4453
end

app/models/detector/suggested_resource_pattern.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ def check_patterns(phrase)
2727
sps << {
2828
shortcode: sp.shortcode,
2929
title: sp.title,
30-
url: sp.url
30+
url: sp.url,
31+
category: sp.category,
32+
confidence: sp.confidence
3133
}
3234
@detections = sps
3335
end
@@ -39,7 +41,7 @@ def check_patterns(phrase)
3941
# @note There are multiple patterns within SuggestedPattern records. Each check is capable of generating
4042
# a separate Detection record.
4143
#
42-
# @return nil
44+
# @return Hash with keys :category and :confidence (or nil)
4345
def self.record(term)
4446
sp = Detector::SuggestedResourcePattern.new(term.phrase)
4547

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

54-
nil
57+
# If a category hasn't been set, nil is better than a confidence with no category
58+
return if sp.detections.first[:category].blank?
59+
60+
{
61+
category: sp.detections.first[:category],
62+
confidence: sp.detections.first[:confidence]
63+
}
5564
end
5665
end
5766
end

app/models/suggested_pattern.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@
44
#
55
# Table name: suggested_patterns
66
#
7-
# id :integer not null, primary key
8-
# title :string not null
9-
# url :string not null
10-
# pattern :string not null
11-
# shortcode :string not null
12-
# created_at :datetime not null
13-
# updated_at :datetime not null
7+
# id :integer not null, primary key
8+
# title :string not null
9+
# url :string not null
10+
# pattern :string not null
11+
# shortcode :string not null
12+
# created_at :datetime not null
13+
# updated_at :datetime not null
14+
# category_id :integer
15+
# confidence :float default(0.9)
1416
#
1517
class SuggestedPattern < ApplicationRecord
1618
validates :title, presence: true
1719
validates :url, presence: true
1820
validates :pattern, presence: true, uniqueness: true
1921
validates :shortcode, presence: true, uniqueness: true
22+
23+
belongs_to :category, optional: true
2024
end

app/models/suggested_resource.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@
44
#
55
# Table name: suggested_resources
66
#
7-
# id :integer not null, primary key
8-
# title :string
9-
# url :string
10-
# created_at :datetime not null
11-
# updated_at :datetime not null
7+
# id :integer not null, primary key
8+
# title :string
9+
# url :string
10+
# created_at :datetime not null
11+
# updated_at :datetime not null
12+
# category_id :integer
13+
# confidence :float default(0.9)
1214
#
13-
# SuggestedResource stores custom hints that we want to send to the
14-
# user in response to specific strings. For example, a search for "web of
15-
# science" should be met with our custom login link to Web of Science via MIT.
1615
class SuggestedResource < ApplicationRecord
1716
has_many :terms, dependent: :nullify
1817
has_many :fingerprints, through: :terms, dependent: :nullify
1918

19+
belongs_to :category, optional: true
20+
2021
# This replaces all current SuggestedResource records with a new set from an imported CSV.
2122
#
2223
# @note This method is called by the suggested_resource:reload rake task.

0 commit comments

Comments
 (0)