Class: Skyline::ArticleVersion

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/skyline/article_version.rb

Direct Known Subclasses

Publication, Variant

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *params, &block) ⇒ Object

Method missing implementation so we can call article_version.page to get the Page or article_version.xxx to get Xxx



77
78
79
80
81
82
83
# File 'app/models/skyline/article_version.rb', line 77

def method_missing(method,*params,&block)
  if method.to_s == self.article.class.name.demodulize.underscore
    self.article
  else
    super
  end
end

Instance Method Details

#build_data(data_attributes) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
20
# File 'app/models/skyline/article_version.rb', line 15

def build_data(data_attributes)
  params = data_attributes.dup
  raise ArgumentError, "Missing class parameter when building data" unless params["class"]
  klass = params.delete("class")
  self.data = klass.constantize.new(params)
end

#cloneObject



26
27
28
29
30
31
32
33
# File 'app/models/skyline/article_version.rb', line 26

def clone
  returning super do |clone|
    clone.created_at = nil
    clone.updated_at = nil
    clone.sections = self.sections.collect{|section| section.clone}
    clone.data = self.data.clone
  end
end

#clone_to_class(klass_or_proxy) ⇒ Object

Clones this object and makes it have another class



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/skyline/article_version.rb', line 36

def clone_to_class(klass_or_proxy)
  clone = klass_or_proxy.build

  attrs = clone_attributes(:read_attribute_before_type_cast)
  attrs.delete(self.class.primary_key)
  attrs.delete("created_at")
  attrs.delete("updated_at")
  attrs["type"] = clone.type
  clone.send :instance_variable_set, '@attributes', attrs
  
  clone.variant = self if clone.respond_to?(:variant)  # only call clone.variant= for publications
  clone.sections = self.sections.collect{|section| section.clone}
  clone.data = self.data.clone
  clone
end

#data_with_buildObject

Custom data method that ensures that you always have a data object if this article needs one.



54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/skyline/article_version.rb', line 54

def data_with_build
  return self.data_without_build unless self.article  # article has been destroyed
  return self.data_without_build unless self.article.data_class
  if current_data = self.data_without_build
    return current_data
  else
    current_data = self.article.data_class.new 
    self.data = current_data unless self.frozen?
    return current_data
  end
end

#respond_to?(method, include_priv = false) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
# File 'app/models/skyline/article_version.rb', line 67

def respond_to?(method, include_priv = false)
  if method.to_s == self.article.class.name.demodulize.underscore
    self.article
  else
    super
  end
end

#to_textObject



22
23
24
# File 'app/models/skyline/article_version.rb', line 22

def to_text  
  self.sections.collect{|s| s.to_text}.join(" ").squeeze(" ")
end