Class: Skyline::Variant

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ArticleVersion

#build_data, #clone, #clone_to_class, #data_with_build, #method_missing, #respond_to?, #to_text

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



138
139
140
141
# File 'app/models/skyline/variant.rb', line 138

def destroy_with_removing_page
  self.destroy_without_removing_page
  self.article.destroy if self.article.variants(true).empty?
end

#destroyable?Boolean

Returns:

  • (Boolean)


129
130
131
132
133
134
135
136
# File 'app/models/skyline/variant.rb', line 129

def destroyable?
  if self.article.andand.published_publication
    self.article.published_publication.variant != self
  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)



122
123
124
125
126
127
# File 'app/models/skyline/variant.rb', line 122

def edit_by!(user, options = {})
  options.reverse_merge! :force => false
  options[:new_editor] = true
  user_id = user.kind_of?(Skyline::User) ? 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)


109
110
111
112
113
# File 'app/models/skyline/variant.rb', line 109

def editable_by?(user)
  user_id = user.kind_of?(Skyline::User) ? 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)


93
94
95
96
# File 'app/models/skyline/variant.rb', line 93

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



80
81
82
# File 'app/models/skyline/variant.rb', line 80

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
# 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.prepare_data_to_be_published!(false)

    published_publication = self.clone_to_class(self.article.publications)
    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
  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)


86
87
88
89
# File 'app/models/skyline/variant.rb', line 86

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