Method: Cms::Behaviors::Publishing::InstanceMethods#publish!

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

#publish!Object

Force the publishing of this block.

Warning: The behavior of calling the following on an existing block:

block.save_on_publish = true
block.save!

Is different than calling:

block.publish!

And it probably shouldn’t be. Try to merge the ‘else’ with the ‘Versioning#create_or_update’ method to eliminate duplication.

In addition, after_publish is NOT called if you do:

block.save_on_publish = true
block.save!

which will cause problems if blocks are updated via the method (like with the UI)



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

def publish!
  if new_record?
    self.publish_on_save = true
    save!
  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_values, false, false, 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)
        end
      else
        connection.update(
          "UPDATE #{self.class.quoted_table_name} " +
          "SET published = #{connection.quote(true, self.class.columns_hash["published"])} " +
          "WHERE #{connection.quote_column_name(self.class.primary_key)} = #{quote_value(id)}",
          "#{self.class.name.demodulize} Publish"
        )
      end
      after_publish if respond_to?(:after_publish)
    end
    self.published = true
  end
end