Module: Transient::ActiveRecordExtensions::InstanceMethods
- Defined in:
- lib/transient/active_record_extensions.rb
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
- #check_and_set_effective_at ⇒ Object
-
#current? ⇒ Boolean
Alias for effective?.
-
#effective? ⇒ Boolean
Returns true if this is currently effective (ie. now is within the effective range), otherwise false.
-
#effective_at_earlier_than_expiring_at ⇒ Object
Validates this record’s effective dates occur in correct sequence (ie. effective_at is before expiring_at).
-
#effective_through ⇒ Object
The range this record is effective wihtin.
-
#effective_through=(value) ⇒ Object
Sets the range this record is effective within.
-
#expire! ⇒ Object
Expires and saves this record.
-
#expired? ⇒ Boolean
Returns true if this record is expired, otherwise false.
-
#expires_at ⇒ Object
The date this record expires.
-
#future? ⇒ Boolean
Returns true if this record is not yet effective, but will become effective at some point in the future, otherwise false.
-
#past? ⇒ Boolean
Alias for expired?.
-
#permanent? ⇒ Boolean
Returns true if this record’s exiring_at date is equivalent to DateTime.end_of, otherwise false.
Class Method Details
.included(base) ⇒ Object
:nodoc:
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/transient/active_record_extensions.rb', line 22 def self.included( base ) #:nodoc: base.instance_eval do def is_active_record_3? respond_to? :where end def scope_method_name is_active_record_3? ? :scope : :named_scope end end base.validates_presence_of :effective_at #base.validates_presence_of :expiring_at base.send( base.scope_method_name, :effective, lambda { { :conditions => ["effective_at <= ? AND (expiring_at IS NULL OR expiring_at > ?)", DateTime.now.utc, DateTime.now.utc] } } ) if base.is_active_record_3? base.before_validation :check_and_set_effective_at, :on => :create else base.before_validation_on_create :check_and_set_effective_at end protected def check_and_set_effective_at self.effective_at = Time.zone.now if self.effective_at.nil? end public # Validates this record's effective dates occur in correct sequence (ie. effective_at is before # expiring_at). # def effective_at_earlier_than_expiring_at return unless self.effective_at && self.expiring_at unless self.effective_at.to_datetime <= self.expires_at.to_datetime self.errors.add( :effective_through, "effective at should be earlier than expiring at" ) end super unless self.class.is_active_record_3? end base.validate :effective_at_earlier_than_expiring_at # The date this record expires. Returns DateTime.end_of for records that have a # nil value in the database. # def expires_at return self.expiring_at.nil? ? DateTime.end_of : self.expiring_at end # The range this record is effective wihtin. # def effective_through self.effective_at.to_datetime..self.expires_at.to_datetime end # Sets the range this record is effective within. # def effective_through=( value ) self.effective_at = value.begin.to_datetime self.expiring_at = value.end.to_datetime end # Returns true if this record's exiring_at date is equivalent to DateTime.end_of, otherwise false. # def permanent? self.expiring_at.to_datetime == DateTime.end_of end # Returns true if this is currently effective (ie. now is within the effective range), otherwise false. # def effective? self.effective_through === DateTime.now end # Returns true if this record is expired, otherwise false. # def expired? self.expires_at.to_datetime < DateTime.now end # Expires and saves this record. # def expire! before_expire! if self.respond_to?( :before_expire! ) self.expiring_at = DateTime.now self.save after_expire! if self.respond_to?( :after_expire! ) end # Returns true if this record is not yet effective, but will become effective at some point in the # future, otherwise false. # def future? self.effective_at.to_datetime > DateTime.now end # Alias for effective?. # def current? self.effective? end # Alias for expired?. # def past? self.expired? end end |
Instance Method Details
#check_and_set_effective_at ⇒ Object
50 51 52 |
# File 'lib/transient/active_record_extensions.rb', line 50 def check_and_set_effective_at self.effective_at = Time.zone.now if self.effective_at.nil? end |
#current? ⇒ Boolean
Alias for effective?.
126 127 128 |
# File 'lib/transient/active_record_extensions.rb', line 126 def current? self.effective? end |
#effective? ⇒ Boolean
Returns true if this is currently effective (ie. now is within the effective range), otherwise false.
98 99 100 |
# File 'lib/transient/active_record_extensions.rb', line 98 def effective? self.effective_through === DateTime.now end |
#effective_at_earlier_than_expiring_at ⇒ Object
Validates this record’s effective dates occur in correct sequence (ie. effective_at is before expiring_at).
59 60 61 62 63 64 65 66 67 |
# File 'lib/transient/active_record_extensions.rb', line 59 def effective_at_earlier_than_expiring_at return unless self.effective_at && self.expiring_at unless self.effective_at.to_datetime <= self.expires_at.to_datetime self.errors.add( :effective_through, "effective at should be earlier than expiring at" ) end super unless self.class.is_active_record_3? end |
#effective_through ⇒ Object
The range this record is effective wihtin.
79 80 81 |
# File 'lib/transient/active_record_extensions.rb', line 79 def effective_through self.effective_at.to_datetime..self.expires_at.to_datetime end |
#effective_through=(value) ⇒ Object
Sets the range this record is effective within.
85 86 87 88 |
# File 'lib/transient/active_record_extensions.rb', line 85 def effective_through=( value ) self.effective_at = value.begin.to_datetime self.expiring_at = value.end.to_datetime end |
#expire! ⇒ Object
Expires and saves this record.
110 111 112 113 114 115 |
# File 'lib/transient/active_record_extensions.rb', line 110 def expire! before_expire! if self.respond_to?( :before_expire! ) self.expiring_at = DateTime.now self.save after_expire! if self.respond_to?( :after_expire! ) end |
#expired? ⇒ Boolean
Returns true if this record is expired, otherwise false.
104 105 106 |
# File 'lib/transient/active_record_extensions.rb', line 104 def expired? self.expires_at.to_datetime < DateTime.now end |
#expires_at ⇒ Object
The date this record expires. Returns DateTime.end_of for records that have a nil value in the database.
73 74 75 |
# File 'lib/transient/active_record_extensions.rb', line 73 def expires_at return self.expiring_at.nil? ? DateTime.end_of : self.expiring_at end |
#future? ⇒ Boolean
Returns true if this record is not yet effective, but will become effective at some point in the future, otherwise false.
120 121 122 |
# File 'lib/transient/active_record_extensions.rb', line 120 def future? self.effective_at.to_datetime > DateTime.now end |
#past? ⇒ Boolean
Alias for expired?.
132 133 134 |
# File 'lib/transient/active_record_extensions.rb', line 132 def past? self.expired? end |