Module: Publishable::ClassMethods
- Defined in:
- lib/publishable.rb
Overview
Define scopes and methods for querying and manipulating Publishables.
Query scopes added to publishable models collapse
-
.published ⇒ Object
Query scope added to publishables that can be used to find published records.
-
.recent ⇒ Object
Query scope added to publishables that can be used to lookup records which are currently published.
-
.unpublished ⇒ Object
Query scope added to publishables that can be used find records which are not published.
-
.upcoming ⇒ Object
Query scope added to publishables that can be used to lookup records which are not currently published.
Instance methods added to publishable models collapse
-
#publish ⇒ Object
Publish this object.
-
#publish! ⇒ Object
Publish this object, then immediately save it to the database.
-
#published? ⇒ Boolean
Is this object published?.
-
#unpublish ⇒ Object
Un-publish this object, i.e.
-
#unpublish! ⇒ Object
Un-publish this object, then immediately save it to the database.
-
#unpublished? ⇒ Boolean
Is this object not published?.
Instance Method Summary collapse
-
#publishable(options = {}) ⇒ Object
DSL method to link this behavior into your model.
Class Method Details
.published ⇒ Object
Query scope added to publishables that can be used to find published records. For Date/DateTime publishables, you can pass a specific date on which the results should be published.
|
# File 'lib/publishable.rb', line 194
|
.recent ⇒ Object
Query scope added to publishables that can be used to lookup records which are currently published. The results are returned in descending order based on the published date/time.
|
# File 'lib/publishable.rb', line 212
|
.unpublished ⇒ Object
Query scope added to publishables that can be used find records which are not published. For Date/DateTime publishables, you can pass a specific date on which the results should not have been published.
|
# File 'lib/publishable.rb', line 204
|
.upcoming ⇒ Object
Query scope added to publishables that can be used to lookup records which are not currently published. The results are returned in ascending order based on the published date/time.
|
# File 'lib/publishable.rb', line 220
|
Instance Method Details
#publish ⇒ Object
Publish this object. For a Boolean publish field, the field is set to true; for a Date/DateTime field, the field is set to the given Date/Time or to the current date/time.
|
# File 'lib/publishable.rb', line 246
|
#publish! ⇒ Object
Publish this object, then immediately save it to the database.
|
# File 'lib/publishable.rb', line 253
|
#publishable(options = {}) ⇒ Object
DSL method to link this behavior into your model. In your ActiveRecord model class, add publishable
to include the scopes and methods for publishable objects.
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/publishable.rb', line 39 def publishable( = {}) return unless table_exists? column_name = ([:on] || :published).to_sym # silently ignore a missing column - since bombing on a missing column can make re-running migrations very hard return unless self.columns_hash[column_name.to_s].present? column_type = self.columns_hash[column_name.to_s].type if respond_to?(:scope) # define published/unpublished scope case column_type when :date scope :published, lambda { |*args| on_date = args[0] || Date.current where(arel_table[column_name].not_eq(nil)).where(arel_table[column_name].lteq(on_date)) } scope :unpublished, lambda { |*args| on_date = args[0] || Date.current where(arel_table[column_name].not_eq(nil)).where(arel_table[column_name].gt(on_date)) } when :datetime scope :published, lambda { |*args| at_time = args[0] || Time.now where(arel_table[column_name].not_eq(nil)).where(arel_table[column_name].lteq(at_time.utc)) } scope :unpublished, lambda { |*args| at_time = args[0] || Time.now where(arel_table[column_name].not_eq(nil)).where(arel_table[column_name].gt(at_time.utc)) } when :boolean scope :published, lambda { where(column_name => true) } scope :unpublished, lambda { where(column_name => false) } else raise ActiveRecord::ConfigurationError, "Invalid column_type #{column_type} for Publishable column on model #{self.name}" end # define recent/upcoming scopes if [:date, :datetime].include? column_type scope :recent, lambda { |*args| how_many = args[0] || nil col_name = arel_table[column_name].name published.limit(how_many).order("#{col_name} DESC") } scope :upcoming, lambda { |*args| how_many = args[0] || nil col_name = arel_table[column_name].name unpublished.limit(how_many).order("#{col_name} ASC") } end end case column_type when :datetime class_eval <<-EVIL, __FILE__, __LINE__ + 1 def published?(_when = Time.now) #{column_name} ? #{column_name} <= _when : false end def unpublished?(_when = Time.now) !published?(_when) end def publish(_when = Time.now) self.#{column_name} = _when unless published?(_when) end def publish!(_when = Time.now) publish(_when) && (!respond_to?(:save) || save) end def unpublish() self.#{column_name} = nil true end def unpublish!() unpublish() && (!respond_to?(:save) || save) end EVIL when :date class_eval <<-EVIL, __FILE__, __LINE__ + 1 def published?(_when = Date.current) #{column_name} ? #{column_name} <= _when : false end def unpublished?(_when = Date.current) !published?(_when) end def publish(_when = Date.current) self.#{column_name} = _when unless published?(_when) end def publish!(_when = Date.current) publish(_when) && (!respond_to?(:save) || save) end def unpublish() self.#{column_name} = nil end def unpublish!() unpublish() && (!respond_to?(:save) || save) end EVIL when :boolean class_eval <<-EVIL, __FILE__, __LINE__ + 1 def published?() #{column_name} end def unpublished?() !published?() end def publish() self.#{column_name} = true end def publish!() publish() save if respond_to?(:save) end def unpublish() self.#{column_name} = false end def unpublish!() unpublish() save if respond_to?(:save) end EVIL else raise ActiveRecord::ConfigurationError, "Invalid column_type #{column_type} for Publishable column on model #{self.name}" end end |
#published? ⇒ Boolean
Is this object published?
|
# File 'lib/publishable.rb', line 232
|
#unpublish ⇒ Object
Un-publish this object, i.e. set it to not be published. For a Boolean publish field, the field is set to false; for a Date/DateTime field, the field is cleared.
|
# File 'lib/publishable.rb', line 258
|
#unpublish! ⇒ Object
Un-publish this object, then immediately save it to the database.
|
# File 'lib/publishable.rb', line 263
|
#unpublished? ⇒ Boolean
Is this object not published?
|
# File 'lib/publishable.rb', line 239
|