mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-02-05 22:44:22 +08:00
Raise an exception if required methods for ViewableModel are not implemented [SCI-3436]
This commit is contained in:
parent
097639e71e
commit
ae1a63037b
1 changed files with 11 additions and 2 deletions
|
@ -3,14 +3,23 @@
|
|||
module ViewableModel
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
has_many :view_states, as: :viewable, dependent: :destroy
|
||||
end
|
||||
|
||||
# This module requres that the class which includes it implements these methods:
|
||||
# => default_view_state, returning hash with default state representation
|
||||
# => validate_view_state(view_state), custom validator for the state hash
|
||||
|
||||
included do
|
||||
has_many :view_states, as: :viewable, dependent: :destroy
|
||||
def default_view_state
|
||||
raise NotImplementedError, 'default_view_state should be implemented!'
|
||||
end
|
||||
|
||||
def validate_view_state(view_state)
|
||||
raise NotImplementedError, 'validate_view_state(view_state) should be implemented!'
|
||||
end
|
||||
|
||||
|
||||
def current_view_state(user)
|
||||
state = view_states.where(user: user).take
|
||||
state || view_states.create!(user: user, state: default_view_state)
|
||||
|
|
Loading…
Reference in a new issue