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

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

Instance Method Summary collapse

Instance Method Details

#live?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/cms/behaviors/publishing.rb', line 116

def live?
  self.class.versioned? ? live_version.version == draft.version && published? : true
end

#publishObject



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

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cms/behaviors/publishing.rb', line 68

def publish!
  if new_record?
    self.publish_on_save = true
    save!
  else
    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(:attributes_with_quotes, false, false, self.class.versioned_columns)

          # Doing the SQL ourselves to avoid callbacks
          connection.update(
            "UPDATE #{self.class.quoted_table_name} " +
            "SET #{quoted_comma_pair_list(connection, quoted_attributes)} " +
            "WHERE #{connection.quote_column_name(self.class.primary_key)} = #{quote_value(id)}",
            "#{self.class.name} Publish"
          )
        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} Publish"
        )
      end
      after_publish if respond_to?(:after_publish)
    end
    self.published = true
  end
end

#publish_for_non_versionedObject



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

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)


43
44
45
46
47
48
49
# File 'lib/cms/behaviors/publishing.rb', line 43

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

#statusObject



108
109
110
# File 'lib/cms/behaviors/publishing.rb', line 108

def status
  live? ? :published : :draft
end

#status_nameObject



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

def status_name
  status.to_s.titleize
end