Module: Cms::Behaviors::Publishing::InstanceMethods

Defined in:
lib/cms/behaviors/publishing.rb

Instance Method Summary collapse

Instance Method Details

#as=(status) ⇒ Object

Can specify whether to save this block as a draft using a terser syntax. These two calls behave identically

- Cms::HtmlBlock.create(name: "Shorter", as: :draft)
- Cms::HtmlBlock.create(name: "Longer",  publish_on_save: false)

Parameters:

  • status (Symbol)

    :draft to not publish on the next save. All other values are ignored.



51
52
53
54
55
# File 'lib/cms/behaviors/publishing.rb', line 51

def as=(status)
  if status == :draft
    self.publish_on_save = false
  end
end

#draft?Boolean

Determines if this resource is in draft mode or not. Opposite of #live?

Returns:

  • (Boolean)

    true if the latest version of the resource is a draft.



180
181
182
# File 'lib/cms/behaviors/publishing.rb', line 180

def draft?
  !live?
end

#live?Boolean

Determines if this resource is in published or not.

Returns:

  • (Boolean)

    true if the latest version of the resource is published.



187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/cms/behaviors/publishing.rb', line 187

def live?
  if self.class.versioned?
    unless persisted?
      return false
    end
    if respond_to?(:latest_version) && self.latest_version
      version == latest_version && published?
    else
      live_version.version == draft.version && published?
    end
  else
    true
  end
end

#publishBoolean

Publishes the latest previously saved version of content as published. This will not create a new version, and will not persist changes made to a record.

Returns:

  • (Boolean)

    true if there was a draft record that was published, false otherwise.



101
102
103
104
105
106
107
# File 'lib/cms/behaviors/publishing.rb', line 101

def publish
  publish!
rescue Exception => e
  Cms::ErrorHandling::NotifierService.notify e
  logger.warn("Could not publish, #{e.class}: #{e.message}\n#{e.backtrace.join("\n")}")
  false
end

#publish!Boolean

Publishes the latest draft version of a block. See .publish for more documentation. Can throw errors if publishing failed for unexpected reasons. Note: Having separate .publish! and .publish methods is probably no longer necessary. In practice, only .publish is probably needed.

Returns:

  • (Boolean)

    true if the block had a draft that was published, false otherwise.



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
# File 'lib/cms/behaviors/publishing.rb', line 119

def publish!
  did_publish = false
  if new_record?
    ActiveSupport::Deprecation.warn "Calling .publish! on a new record no longer saves the record. Call '.save' to persist and publish the record.", caller
  else
    # Do this for publishing existing blocks.
    transaction do
      if self.class.versioned?
        d = draft

        # We only need to publish if this isn't already published
        # or the draft version is greater than the live version
        if !self.published? || d.version > self.version

          d.update_attributes(:published => true)

          # copy values from the draft to the main record
          quoted_attributes = d.send(:arel_attributes_with_values_for_update, self.class.versioned_columns)

          #the values from the draft MAY have a relation of the versioned module
          #as opposed to the actual class itself
          #eg Page::Version, and not Page
          #so remap to the actual arel_table´
          #I haven't figured out why this is, but I know it happens when you call save! on Page
          #during seeding of data
          if self.class.arel_table.name != quoted_attributes.keys[0].relation.name
            quoted_attributes = quoted_attributes.inject({}) { |hash, pair| hash[self.class.arel_table[pair[0].name]] = pair[1]; hash }
          end

          # Doing the SQL ourselves to avoid callbacks
          self.class.unscoped.where(self.class.arel_table[self.class.primary_key].eq(id)).arel.update(quoted_attributes)
          did_publish = true
        end
      else
        self.class.connection.update(
            "UPDATE #{self.class.quoted_table_name} " +
                "SET published = #{self.class.connection.quote(true, self.class.columns_hash["published"])} " +
                "WHERE #{self.class.connection.quote_column_name(self.class.primary_key)} = #{self.class.quote_value(id)}",
            "#{self.class.name.demodulize} Publish"
        )
        did_publish = true
      end
      after_publish if respond_to?(:after_publish)
    end
    self.published = true
  end
  did_publish
end

#publish_for_non_versionedObject



80
81
82
83
84
85
86
87
# File 'lib/cms/behaviors/publishing.rb', line 80

def publish_for_non_versioned
  unless self.class.versioned?
    if @publish_on_save
      publish
      @publish_on_save = nil
    end
  end
end

#publish_if_neededObject



89
90
91
92
93
94
95
# File 'lib/cms/behaviors/publishing.rb', line 89

def publish_if_needed
  if publish_on_save
    publish
  else
    self.publish_on_save = true
  end
end

#publish_on_saveBoolean

Whether or not this object will be published the next time ‘.save’ is called.

Returns:

  • (Boolean)

    True unless explicitly set otherwise.



59
60
61
62
63
64
# File 'lib/cms/behaviors/publishing.rb', line 59

def publish_on_save
  if @publish_on_save.nil?
    @publish_on_save = true
  end
  @publish_on_save
end

#publish_on_save=(publish) ⇒ Object

Set whether or not this object will be published next time ‘.save’ is called. This status resets to true after calling ‘.save’



68
69
70
# File 'lib/cms/behaviors/publishing.rb', line 68

def publish_on_save=(publish)
  @publish_on_save = publish
end

#publishable?Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
# File 'lib/cms/behaviors/publishing.rb', line 72

def publishable?
  if self.class.connectable?
    new_record? ? connect_to_page_id.blank? : connected_page_count < 1
  else
    true
  end
end

#save_draftObject

Saves a draft copy of this content item. This will create a new record in the _versions table for this item, but will not update the existing published record.



111
112
113
114
# File 'lib/cms/behaviors/publishing.rb', line 111

def save_draft
  self.publish_on_save = false
  save
end

#statusObject



168
169
170
171
# File 'lib/cms/behaviors/publishing.rb', line 168

def status
  return @status if @status
  @status = live? ? :published : :draft
end

#status_nameObject



173
174
175
# File 'lib/cms/behaviors/publishing.rb', line 173

def status_name
  status.to_s.titleize
end