Module: Mongoid::Publishable::InstanceMethods

Defined in:
lib/mongoid/publishable.rb

Instance Method Summary collapse

Instance Method Details

#has_publisher_id?Boolean

returns whether or not the publisher is present

Returns:

  • (Boolean)


117
118
119
# File 'lib/mongoid/publishable.rb', line 117

def has_publisher_id?
  !!send(publisher_column)
end

#meets_custom_publishing_conditions?Boolean

returns true if there are no conditions, or the resulting yield is true

Returns:

  • (Boolean)


122
123
124
# File 'lib/mongoid/publishable.rb', line 122

def meets_custom_publishing_conditions?
  publishing_conditions.nil? || publishing_conditions.yield(self)
end

#persist_and_publish(publisher = nil) ⇒ Object

saves to the db, and publishes if possible



43
44
45
46
# File 'lib/mongoid/publishable.rb', line 43

def persist_and_publish(publisher = nil)
  published = publish_via(publisher)
  save && published
end

#persist_and_publish!(publisher = nil) ⇒ Object

saves to the db, and publishes if possible raises an UnpublishedError if unable to publish



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mongoid/publishable.rb', line 50

def persist_and_publish!(publisher = nil)
  # attempt save / publish
  persist_and_publish(publisher)
  # if it was saved to the DB
  if persisted?
    # return true if published, raise exception if not
    published? || raise_unpublished_error
  # if the save failed
  else
    # return false to allow traditional validation
    false
  end
end

#pre_published?Boolean

returns boolean of whether this instance has been published regardless of whether it’s been persisted yet

Returns:

  • (Boolean)


102
103
104
# File 'lib/mongoid/publishable.rb', line 102

def pre_published?
  has_publisher_id? && meets_custom_publishing_conditions?
end

#publish_via(publisher) ⇒ Object

publishes this instance using the id provided



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/mongoid/publishable.rb', line 75

def publish_via(publisher)
  # ensure this isn't published and we have a publisher
  if !published? && publisher
    # load the publisher's foreign key
    value = publisher.send(publisher_foreign_key)
    # update this instance with the key
    self.send("#{publisher_column}=", value)
    # if this now counts as published
    if pre_published?
      # mark as just published
      run_after_publish_callbacks
    end
    # always return true
    true
  else
    false
  end
end

#publish_via!(publisher) ⇒ Object

publishes this instance using the id provided and persists the publishing



96
97
98
# File 'lib/mongoid/publishable.rb', line 96

def publish_via!(publisher)
  publish_via(publisher) && save
end

#published?Boolean

returns boolean of whether this instance has been published

Returns:

  • (Boolean)


107
108
109
# File 'lib/mongoid/publishable.rb', line 107

def published?
  persisted? && pre_published?
end

#publisher_columnObject

delegate publisher column, allow overriding



65
66
67
# File 'lib/mongoid/publishable.rb', line 65

def publisher_column
  @publisher_column || self.class.publisher_column
end

#publisher_foreign_keyObject

delegate foreign key, allow overriding



70
71
72
# File 'lib/mongoid/publishable.rb', line 70

def publisher_foreign_key
  @publisher_foreign_key || self.class.publisher_foreign_key
end

#publishing_conditionsObject

returns a block with custom publishing conditions



127
128
129
# File 'lib/mongoid/publishable.rb', line 127

def publishing_conditions
  self.class.publishing_conditions
end

#raise_unpublished_errorObject

raises an UnpublishedError containing this object as a reference

Raises:



132
133
134
# File 'lib/mongoid/publishable.rb', line 132

def raise_unpublished_error
  raise UnpublishedError.new.tap { |e| e.model = self }, "Unable to publish this #{self.class.name}"
end

#requires_publishing?Boolean

returns whether this instance needs publishing (persisted, not published)

Returns:

  • (Boolean)


112
113
114
# File 'lib/mongoid/publishable.rb', line 112

def requires_publishing?
  persisted? && !pre_published?
end