Class: Skyline::Variant

Inherits:
ArticleVersion
  • Object
show all
Includes:
NestedAttributesPositioning
Defined in:
app/models/skyline/variant.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Skyline::ArticleVersion

Class Method Details

.editor_idle_timeObject



45
46
47
# File 'app/models/skyline/variant.rb', line 45

def editor_idle_time
  30
end

.find_current_editor_for(id) ⇒ Object



18
19
20
21
22
23
24
# File 'app/models/skyline/variant.rb', line 18

def find_current_editor_for(id)
  values = self.connection.select_one("SELECT current_editor_id,current_editor_timestamp, current_editor_since FROM #{self.table_name} WHERE id = #{id.to_i}")
  %w{current_editor_timestamp current_editor_since}.each do |t|
    values[t] = ActiveRecord::ConnectionAdapters::Column.string_to_time(values[t]) if values[t]
  end
  values
end

.update_current_editor(id, editor_id, options = {}) ⇒ Object

Updates the current editor timestamp for a variant

Parameters

id<Integer>

The ID of the variant

editor_id<Integer>

The id of the current editor

Options

:new_editor<Boolean>

If true, we also set “current_editor_since”

:force<Boolean>

Force the takeover, this wil update the version number and render all other editors editing this page unusable (default = false)



35
36
37
38
39
40
41
42
43
# File 'app/models/skyline/variant.rb', line 35

def update_current_editor(id, editor_id, options={})
  options.reverse_merge! :new_editor => false, :force => false
  values = {:current_editor_id => editor_id, :current_editor_timestamp => Time.zone.now.utc}
  values[:current_editor_since] = Time.zone.now.utc if options[:new_editor]
  extra_sql = ""
  extra_sql << ", version = version + 1" if options[:force]
  
  self.connection.update("UPDATE #{self.table_name} SET #{ sanitize_sql_for_assignment(values) }#{extra_sql} WHERE id = #{id.to_i}")
end

Instance Method Details

#destroy_with_removing_pageObject



146
147
148
149
150
151
# File 'app/models/skyline/variant.rb', line 146

def destroy_with_removing_page
  Skyline::Article.transaction do
    self.destroy_without_removing_page
    self.article.destroy if self.article.variants(true).empty?
  end
end

#destroyable?Boolean

Returns:

  • (Boolean)


133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/models/skyline/variant.rb', line 133

def destroyable?
  if self.article.andand.published_publication
    # This variant should not be the published variant
    self.article.published_publication.variant != self
  elsif self.article && self.article.variants.size == 1
    # If this is the only variant we should check if the article can be destroyed
    self.article.destroyable?
  else
    # also yield true if self.article doesn't exist (this happens when the article is already destroyed)
    true
  end
end

#edit_by!(user, options = {}) ⇒ Object

Set a new user that will be editing this page.

Parameters

user<User,Integer>

A user instacne or a user id

Options

:force<Boolean>

Force the takeover, this wil update the version number and render all other editors editing this page unusable (default = false)



126
127
128
129
130
131
# File 'app/models/skyline/variant.rb', line 126

def edit_by!(user, options = {})
  options.reverse_merge! :force => false
  options[:new_editor] = true
  user_id = user.kind_of?(Skyline::Configuration.user_class) ? user.id : user
  self.class.update_current_editor(self.id, user_id, options)
end

#editable_by?(user) ⇒ Boolean

Check wether or not this page is editable by user Checks the following:

* page.locked?
* Skyline::Configuration.enable_enforce_only_one_user_editing
* currently_editable_by?

Parameters

user<User,Integer>

A user instance or a user id

Returns

true,false

true if the user is allowed to edit.

Returns:

  • (Boolean)


113
114
115
116
117
# File 'app/models/skyline/variant.rb', line 113

def editable_by?(user)
  user_id = user.kind_of?(Skyline::Configuration.user_class) ? user.id : user
  return true unless Skyline::Configuration.enable_enforce_only_one_user_editing
  self.current_editor_id.nil? || self.current_editor_timestamp.nil? || self.current_editor_id == user_id || self.class.editor_idle_time < (Time.zone.now - self.current_editor_timestamp)
end

#identical_published_variant?(options = {}) ⇒ Boolean

Options

:article<Article>

If you’ve already loaded the article somewhere else, you can pass it

Returns:

  • (Boolean)


97
98
99
100
# File 'app/models/skyline/variant.rb', line 97

def identical_published_variant?(options = {})
  options.reverse_merge! :article => self.article
  self.published_variant?(options) && self.version == options[:article].published_publication.version
end

#prepare_data_to_be_published!(value = true) ⇒ Object



84
85
86
# File 'app/models/skyline/variant.rb', line 84

def prepare_data_to_be_published!(value = true)
  self.data.to_be_published = value if self.data.respond_to?(:to_be_published=)
end

#publishObject

Raises:

  • (StandardError)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/skyline/variant.rb', line 50

def publish
  self.prepare_data_to_be_published!(true)
  
  raise StandardError, "can't be published if its dirty" if self.changed? || self.data.changed?
  
  if self.valid?
    self.article.run_callbacks :publication do
      self.prepare_data_to_be_published!(false)

      published_publication = self.dup_to_class(self.article.publications)
      published_publication.created_at = Time.now
      published_publication.updated_at = Time.now
      published_publication.save

      self.article.published_publication = published_publication
      self.article.url_part = published_publication.data.url_part if published_publication.data.respond_to?(:url_part)
      self.article.published_publication_data = published_publication.data
      self.article.set_default_variant(self)
      self.article.save!

      unless self.article.keep_history?
        self.article.publications.each do |publication|
          publication.destroy if publication != published_publication
        end
      end

      published_publication
    end
  else
    self.prepare_data_to_be_published!(false)
    false
  end
end

#published_variant?(options = {}) ⇒ Boolean

Options

:article<Article>

If you’ve already loaded the article somewhere else, you can pass it

Returns:

  • (Boolean)


90
91
92
93
# File 'app/models/skyline/variant.rb', line 90

def published_variant?(options = {})
  options.reverse_merge! :article => self.article
  options[:article].published_publication.andand.variant_id == self.id
end