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

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

Instance Method Summary collapse

Instance Method Details

#live?Boolean

Returns:

  • (Boolean)


142
143
144
145
146
147
148
149
150
151
152
# File 'lib/cms/behaviors/publishing.rb', line 142

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

#publishObject



64
65
66
67
68
69
70
# File 'lib/cms/behaviors/publishing.rb', line 64

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

#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

#publish_for_non_versionedObject



55
56
57
58
59
60
61
62
# File 'lib/cms/behaviors/publishing.rb', line 55

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

#publishable?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
# File 'lib/cms/behaviors/publishing.rb', line 47

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

#statusObject



133
134
135
136
# File 'lib/cms/behaviors/publishing.rb', line 133

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

#status_nameObject



138
139
140
# File 'lib/cms/behaviors/publishing.rb', line 138

def status_name
  status.to_s.titleize
end