Class: Decidim::DecidimAwesome::Lock
- Inherits:
-
Object
- Object
- Decidim::DecidimAwesome::Lock
- Defined in:
- lib/decidim/decidim_awesome/lock.rb
Instance Attribute Summary collapse
-
#lock_time ⇒ Object
readonly
Returns the value of attribute lock_time.
-
#organization ⇒ Object
readonly
Returns the value of attribute organization.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Instance Method Summary collapse
- #config ⇒ Object
- #get!(resource) ⇒ Object
-
#initialize(organization, lock_time: DecidimAwesome.lock_time) ⇒ Lock
constructor
A new instance of Lock.
- #locked?(resource) ⇒ Boolean
- #release!(resource) ⇒ Object
Constructor Details
#initialize(organization, lock_time: DecidimAwesome.lock_time) ⇒ Lock
Returns a new instance of Lock.
6 7 8 9 |
# File 'lib/decidim/decidim_awesome/lock.rb', line 6 def initialize(organization, lock_time: DecidimAwesome.lock_time) @organization = organization @lock_time = lock_time end |
Instance Attribute Details
#lock_time ⇒ Object (readonly)
Returns the value of attribute lock_time.
11 12 13 |
# File 'lib/decidim/decidim_awesome/lock.rb', line 11 def lock_time @lock_time end |
#organization ⇒ Object (readonly)
Returns the value of attribute organization.
11 12 13 |
# File 'lib/decidim/decidim_awesome/lock.rb', line 11 def organization @organization end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
11 12 13 |
# File 'lib/decidim/decidim_awesome/lock.rb', line 11 def resource @resource end |
Instance Method Details
#config ⇒ Object
36 37 38 |
# File 'lib/decidim/decidim_awesome/lock.rb', line 36 def config AwesomeConfig.find_by(var:, organization:) end |
#get!(resource) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/decidim/decidim_awesome/lock.rb', line 13 def get!(resource) @resource = resource if config config.update!(updated_at: Time.current) else AwesomeConfig.create!(var:, organization:) end end |
#locked?(resource) ⇒ Boolean
27 28 29 30 31 32 33 34 |
# File 'lib/decidim/decidim_awesome/lock.rb', line 27 def locked?(resource) @resource = resource return false unless config return true if config.reload.updated_at > lock_time.ago config.destroy! false end |
#release!(resource) ⇒ Object
22 23 24 25 |
# File 'lib/decidim/decidim_awesome/lock.rb', line 22 def release!(resource) @resource = resource config.destroy! if config end |