Class: NoPassword::Link::Base
- Inherits:
-
Object
- Object
- NoPassword::Link::Base
- Includes:
- ActiveModel::Attributes, ActiveModel::Model
- Defined in:
- lib/nopassword/link/base.rb
Overview
Base class for session-stored objects in the Link authentication flow. Provides ActiveModel compatibility and session persistence.
Direct Known Subclasses
Class Method Summary collapse
- .delete(session) ⇒ Object
-
.update(session) ⇒ Object
Update session data, optionally yielding for modifications.
Instance Method Summary collapse
- #delete ⇒ Object
-
#initialize(session, **attributes) ⇒ Base
constructor
A new instance of Base.
- #persisted? ⇒ Boolean
- #save ⇒ Object
- #save! ⇒ Object
-
#save_without_validation ⇒ Object
Save without running validations - useful for partial updates.
Constructor Details
#initialize(session, **attributes) ⇒ Base
Returns a new instance of Base.
9 10 11 12 13 |
# File 'lib/nopassword/link/base.rb', line 9 def initialize(session, **attributes) @session = session data = @session[session_key] || {} super(**data.symbolize_keys.merge(attributes)) end |
Class Method Details
.delete(session) ⇒ Object
43 44 45 46 |
# File 'lib/nopassword/link/base.rb', line 43 def self.delete(session) key = "nopassword_#{name.demodulize.underscore}".to_sym session.delete(key) end |
.update(session) ⇒ Object
Update session data, optionally yielding for modifications
32 33 34 35 36 37 |
# File 'lib/nopassword/link/base.rb', line 32 def self.update(session, **) new(session, **).tap do |instance| yield instance if block_given? instance.save_without_validation end end |
Instance Method Details
#delete ⇒ Object
39 40 41 |
# File 'lib/nopassword/link/base.rb', line 39 def delete @session.delete(session_key) end |
#persisted? ⇒ Boolean
48 49 50 |
# File 'lib/nopassword/link/base.rb', line 48 def persisted? false end |
#save ⇒ Object
15 16 17 18 19 |
# File 'lib/nopassword/link/base.rb', line 15 def save return false unless valid? @session[session_key] = attributes.compact true end |
#save! ⇒ Object
21 22 23 |
# File 'lib/nopassword/link/base.rb', line 21 def save! save || raise(ActiveModel::ValidationError.new(self)) end |
#save_without_validation ⇒ Object
Save without running validations - useful for partial updates
26 27 28 29 |
# File 'lib/nopassword/link/base.rb', line 26 def save_without_validation @session[session_key] = attributes.compact true end |