Module: ActiveRecord::Publishable
- Defined in:
- lib/active_record/publishable.rb,
lib/active_record/publishable/version.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- VERSION =
"0.0.1"
Class Attribute Summary collapse
Class Method Summary collapse
- .disable! ⇒ Object
- .disabled? ⇒ Boolean
- .enable! ⇒ Object
- .included(base) ⇒ Object
- .publish(channel, message) ⇒ Object
Instance Method Summary collapse
- #channel_for_publishing(action) ⇒ Object
- #publish_action(action, options = {}) ⇒ Object
- #serialize_for_publishing(options = {}) ⇒ Object
Class Attribute Details
.redis ⇒ Object
25 26 27 |
# File 'lib/active_record/publishable.rb', line 25 def redis @redis ||= Redis.new end |
Class Method Details
.disable! ⇒ Object
13 14 15 |
# File 'lib/active_record/publishable.rb', line 13 def disable! @disabled = true end |
.disabled? ⇒ Boolean
21 22 23 |
# File 'lib/active_record/publishable.rb', line 21 def disabled? !!@disabled end |
.enable! ⇒ Object
17 18 19 |
# File 'lib/active_record/publishable.rb', line 17 def enable! @disabled = nil end |
.included(base) ⇒ Object
9 10 11 |
# File 'lib/active_record/publishable.rb', line 9 def included(base) base.extend ClassMethods end |
.publish(channel, message) ⇒ Object
29 30 31 |
# File 'lib/active_record/publishable.rb', line 29 def publish(channel, ) redis.publish(channel, ) unless disabled? end |
Instance Method Details
#channel_for_publishing(action) ⇒ Object
64 65 66 |
# File 'lib/active_record/publishable.rb', line 64 def channel_for_publishing(action) "#{self.class.model_name.collection}:#{action}" end |
#publish_action(action, options = {}) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/active_record/publishable.rb', line 46 def publish_action(action, = {}) channel = [:channel] || channel_for_publishing(action) data = serialize_for_publishing(.fetch(:serialize, {})) ActiveRecord::Publishable.publish(channel, data.to_json) end |
#serialize_for_publishing(options = {}) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/active_record/publishable.rb', line 53 def serialize_for_publishing( = {}) opts = .reverse_merge(root: false).except(:with) serializer = .fetch :with do next unless defined? ActiveModel::Serializer ActiveModel::Serializer.serializer_for self end serializer ? serializer.new(self, opts) : as_json(opts) end |