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

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

Instance Method Summary collapse

Instance Method Details

#live?Boolean

Returns:

  • (Boolean)


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

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

#publishObject



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

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



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

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



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

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)


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

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

#statusObject



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

def status
  live? ? :published : :draft
end

#status_nameObject



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

def status_name
  status.to_s.titleize
end