-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
... and introduced a person model. Linked scholars, people (person) and users accordingly.
- Loading branch information
Showing
28 changed files
with
339 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module NameFinder | ||
|
||
extend ActiveSupport::Concern | ||
|
||
included do | ||
ransacker :name do |parent| | ||
Arel::Nodes::NamedFunction.new "concat", [parent.table[:first_name], Arel::Nodes.build_quoted(" "), parent.table[:last_name]] | ||
end | ||
end | ||
|
||
def name | ||
[first_name, last_name].reject(&:blank?).map(&:strip).join(" ") | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Person < ApplicationRecord | ||
|
||
include NameFinder | ||
|
||
has_one :user, inverse_of: :person | ||
|
||
validates :email, presence: true, uniqueness: true | ||
validates_format_of :email, with: Devise.email_regexp | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,64 @@ | ||
class Scholar < ApplicationRecord | ||
|
||
extend Enumerize | ||
include Statesman::Adapters::ActiveRecordQueries | ||
include NameFinder | ||
|
||
class << self | ||
|
||
def transition_class | ||
ScholarTransition | ||
end | ||
|
||
private | ||
|
||
def initial_state | ||
:in_review | ||
end | ||
|
||
end | ||
|
||
paginates_per 45 | ||
|
||
belongs_to :discipline | ||
belongs_to :institute, optional: true | ||
belongs_to :created_by, class_name: "Person" | ||
has_one :organisation | ||
has_many :web_urls, as: :linkable | ||
has_many :scholar_transitions, inverse_of: :scholar, dependent: :destroy | ||
|
||
scope :approved, -> { with_state :approved } | ||
|
||
accepts_nested_attributes_for :organisation, allow_destroy: true | ||
accepts_nested_attributes_for :web_urls, reject_if: :reject_web_urls?, allow_destroy: true | ||
accepts_nested_attributes_for :created_by | ||
|
||
ransacker :name do |parent| | ||
Arel::Nodes::NamedFunction.new "concat", [parent.table[:first_name], Arel::Nodes.build_quoted(" "), parent.table[:last_name]] | ||
end | ||
before_validation :set_created_by, on: :create | ||
|
||
validates :first_name, :last_name, :discipline, presence: true | ||
|
||
def name | ||
[first_name, last_name].reject(&:blank?).map(&:strip).join(" ") | ||
end | ||
enumerize :state, in: ScholarStateMachine.states, default: :in_review, predicates: true, scope: true | ||
|
||
delegate :can_transition_to?, :transition_to!, :transition_to, :current_state, to: :state_machine | ||
|
||
%i[publication personal].each do |code| | ||
define_method("build_#{code}_urls") do | ||
web_urls.build(code: code) if web_urls.none?(&:"#{code}?") | ||
end | ||
end | ||
|
||
def state_machine | ||
@state_machine ||= ScholarStateMachine.new(self, transition_class: ScholarTransition) | ||
end | ||
|
||
private | ||
|
||
def reject_web_urls?(attributes) | ||
attributes["code"] == "personal" && | ||
(attributes["title"].blank? || attributes["url"].blank?) | ||
end | ||
|
||
def set_created_by | ||
self.created_by = User.current&.person || Person.find_by(email: created_by&.email) || created_by | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class ScholarStateMachine | ||
|
||
include Statesman::Machine | ||
|
||
state :in_review, initial: true | ||
state :approved | ||
state :declined | ||
|
||
transition from: :in_review, to: [:approved, :declined] | ||
transition from: :declined, to: :in_review | ||
# TODO: approved scholars can be edited. | ||
# add appropriate transitions for such scenarios. | ||
|
||
guard_transition(to: :declined) do |scholar| | ||
# TODO: validate feedback/reason supplied | ||
end | ||
|
||
after_transition(to: :declined) do |scholar, transition| | ||
# TODO: trigger mail to the person who added the scholar | ||
# with feedback/reason on why it was declined | ||
end | ||
|
||
after_transition do |scholar, transition| | ||
# also store current state on model directly | ||
scholar.update_columns state: transition.to_state | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class ScholarTransition < ApplicationRecord | ||
|
||
extend Enumerize | ||
|
||
belongs_to :scholar, inverse_of: :scholar_transitions | ||
belongs_to :created_by, class_name: "Person" | ||
|
||
validates :scholar, :created_by, presence: true | ||
validates :to_state, inclusion: {in: ScholarStateMachine.states} | ||
|
||
before_validation :set_created_by | ||
after_destroy :update_most_recent, if: :most_recent? | ||
|
||
enumerize :to_state, in: ScholarStateMachine.states, predicates: true | ||
|
||
private | ||
|
||
def update_most_recent | ||
last_transition = scholar.scholar_transitions.order(:sort_key).last | ||
return unless last_transition.present? | ||
last_transition.update_column(:most_recent, true) | ||
end | ||
|
||
def set_created_by | ||
self.created_by_id ||= User.current&.person_id | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.