Class: Content

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Housekeeping, Shared::IsData
Defined in:
app/models/content.rb

Overview

Content is text related blocks, at present it only pertains to Otus. It requires both a Topic and an Otu. Future extensions may be added to use the model for Projects, etc. via STI.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Housekeeping

#has_polymorphic_relationship?

Instance Attribute Details

- (Integer) otu_id

When OtuContent::Text the id of the Otu the content pertains to. At present required.

Returns:

  • (Integer)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/content.rb', line 25

class Content < ActiveRecord::Base
  include Housekeeping
  include Shared::IsData

  has_paper_trail

  belongs_to :otu, inverse_of: :contents
  belongs_to :topic, inverse_of: :contents
  has_one :public_content

  validates_presence_of :text
  validates :topic, presence: true
  validates :otu, presence: true

  # scope :for_otu_page_layout, -> (otu_page_layout_id) {
  #   where('otu_page_layout_id = ?', otu_page_layout.od)
  # }

  # @return [Boolean]
  #    true if this content has been published
  def published?
    self.public_content
  end

  # TODO: this will have to be updated in subclasses.
  def publish
    to_publish = {
      topic: self.topic,
      text:  self.text,
      otu:   self.otu
    }

    self.public_content.delete if self.public_content
    self.public_content = PublicContent.new(to_publish)
    self.save
  end

  def unpublish
    self.public_content.destroy
  end

  # OTU_PAGE_LAYOUTS
  #       V
  # OTU_PAGE_LAYOUT_SECTIONS ^ .otu_page_layout_id v .topic_id
  #       V
  #     TOPICS
  #       V
  #    CONTENTS              v otu_id              ^ .topic_id
  #       ^
  #      OTU
  #
  # Given an otu_page_layout id. find all the topics
  # For this otu_page_layout, find the topics (ControlledVocabularyTerm.of_type(:topic))

  def self.for_page_layout(otu_page_layout_id)
    where('topic_id in (?)', OtuPageLayout.where(id: otu_page_layout_id).first.topics.pluck(:id))
  end

  def self.find_for_autocomplete(params)
    where('text ILIKE ? OR text ILIKE ?', "#{params[:term]}%", "%#{params[:term]}%")
  end

  # @return [CSV]
  # Generate a version of the raw contents table for the given scope 
  # Ripped from http://railscasts.com/episodes/362-exporting-csv-and-excel
  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

end

- (Integer) project_id

the project ID

Returns:

  • (Integer)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/content.rb', line 25

class Content < ActiveRecord::Base
  include Housekeeping
  include Shared::IsData

  has_paper_trail

  belongs_to :otu, inverse_of: :contents
  belongs_to :topic, inverse_of: :contents
  has_one :public_content

  validates_presence_of :text
  validates :topic, presence: true
  validates :otu, presence: true

  # scope :for_otu_page_layout, -> (otu_page_layout_id) {
  #   where('otu_page_layout_id = ?', otu_page_layout.od)
  # }

  # @return [Boolean]
  #    true if this content has been published
  def published?
    self.public_content
  end

  # TODO: this will have to be updated in subclasses.
  def publish
    to_publish = {
      topic: self.topic,
      text:  self.text,
      otu:   self.otu
    }

    self.public_content.delete if self.public_content
    self.public_content = PublicContent.new(to_publish)
    self.save
  end

  def unpublish
    self.public_content.destroy
  end

  # OTU_PAGE_LAYOUTS
  #       V
  # OTU_PAGE_LAYOUT_SECTIONS ^ .otu_page_layout_id v .topic_id
  #       V
  #     TOPICS
  #       V
  #    CONTENTS              v otu_id              ^ .topic_id
  #       ^
  #      OTU
  #
  # Given an otu_page_layout id. find all the topics
  # For this otu_page_layout, find the topics (ControlledVocabularyTerm.of_type(:topic))

  def self.for_page_layout(otu_page_layout_id)
    where('topic_id in (?)', OtuPageLayout.where(id: otu_page_layout_id).first.topics.pluck(:id))
  end

  def self.find_for_autocomplete(params)
    where('text ILIKE ? OR text ILIKE ?', "#{params[:term]}%", "%#{params[:term]}%")
  end

  # @return [CSV]
  # Generate a version of the raw contents table for the given scope 
  # Ripped from http://railscasts.com/episodes/362-exporting-csv-and-excel
  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

end

- (Integer) revision_id

Stubbed placeholder for Revision (sensus taxonomy) model. NOT PRESENTLY USED.

Returns:

  • (Integer)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/content.rb', line 25

class Content < ActiveRecord::Base
  include Housekeeping
  include Shared::IsData

  has_paper_trail

  belongs_to :otu, inverse_of: :contents
  belongs_to :topic, inverse_of: :contents
  has_one :public_content

  validates_presence_of :text
  validates :topic, presence: true
  validates :otu, presence: true

  # scope :for_otu_page_layout, -> (otu_page_layout_id) {
  #   where('otu_page_layout_id = ?', otu_page_layout.od)
  # }

  # @return [Boolean]
  #    true if this content has been published
  def published?
    self.public_content
  end

  # TODO: this will have to be updated in subclasses.
  def publish
    to_publish = {
      topic: self.topic,
      text:  self.text,
      otu:   self.otu
    }

    self.public_content.delete if self.public_content
    self.public_content = PublicContent.new(to_publish)
    self.save
  end

  def unpublish
    self.public_content.destroy
  end

  # OTU_PAGE_LAYOUTS
  #       V
  # OTU_PAGE_LAYOUT_SECTIONS ^ .otu_page_layout_id v .topic_id
  #       V
  #     TOPICS
  #       V
  #    CONTENTS              v otu_id              ^ .topic_id
  #       ^
  #      OTU
  #
  # Given an otu_page_layout id. find all the topics
  # For this otu_page_layout, find the topics (ControlledVocabularyTerm.of_type(:topic))

  def self.for_page_layout(otu_page_layout_id)
    where('topic_id in (?)', OtuPageLayout.where(id: otu_page_layout_id).first.topics.pluck(:id))
  end

  def self.find_for_autocomplete(params)
    where('text ILIKE ? OR text ILIKE ?', "#{params[:term]}%", "%#{params[:term]}%")
  end

  # @return [CSV]
  # Generate a version of the raw contents table for the given scope 
  # Ripped from http://railscasts.com/episodes/362-exporting-csv-and-excel
  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

end

- (String) text

The written content.

Returns:

  • (String)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/content.rb', line 25

class Content < ActiveRecord::Base
  include Housekeeping
  include Shared::IsData

  has_paper_trail

  belongs_to :otu, inverse_of: :contents
  belongs_to :topic, inverse_of: :contents
  has_one :public_content

  validates_presence_of :text
  validates :topic, presence: true
  validates :otu, presence: true

  # scope :for_otu_page_layout, -> (otu_page_layout_id) {
  #   where('otu_page_layout_id = ?', otu_page_layout.od)
  # }

  # @return [Boolean]
  #    true if this content has been published
  def published?
    self.public_content
  end

  # TODO: this will have to be updated in subclasses.
  def publish
    to_publish = {
      topic: self.topic,
      text:  self.text,
      otu:   self.otu
    }

    self.public_content.delete if self.public_content
    self.public_content = PublicContent.new(to_publish)
    self.save
  end

  def unpublish
    self.public_content.destroy
  end

  # OTU_PAGE_LAYOUTS
  #       V
  # OTU_PAGE_LAYOUT_SECTIONS ^ .otu_page_layout_id v .topic_id
  #       V
  #     TOPICS
  #       V
  #    CONTENTS              v otu_id              ^ .topic_id
  #       ^
  #      OTU
  #
  # Given an otu_page_layout id. find all the topics
  # For this otu_page_layout, find the topics (ControlledVocabularyTerm.of_type(:topic))

  def self.for_page_layout(otu_page_layout_id)
    where('topic_id in (?)', OtuPageLayout.where(id: otu_page_layout_id).first.topics.pluck(:id))
  end

  def self.find_for_autocomplete(params)
    where('text ILIKE ? OR text ILIKE ?', "#{params[:term]}%", "%#{params[:term]}%")
  end

  # @return [CSV]
  # Generate a version of the raw contents table for the given scope 
  # Ripped from http://railscasts.com/episodes/362-exporting-csv-and-excel
  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

end

- (Integer) topic_id

When OtuContent::Text the id of the Topic the content pertains to. At present required.

Returns:

  • (Integer)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/content.rb', line 25

class Content < ActiveRecord::Base
  include Housekeeping
  include Shared::IsData

  has_paper_trail

  belongs_to :otu, inverse_of: :contents
  belongs_to :topic, inverse_of: :contents
  has_one :public_content

  validates_presence_of :text
  validates :topic, presence: true
  validates :otu, presence: true

  # scope :for_otu_page_layout, -> (otu_page_layout_id) {
  #   where('otu_page_layout_id = ?', otu_page_layout.od)
  # }

  # @return [Boolean]
  #    true if this content has been published
  def published?
    self.public_content
  end

  # TODO: this will have to be updated in subclasses.
  def publish
    to_publish = {
      topic: self.topic,
      text:  self.text,
      otu:   self.otu
    }

    self.public_content.delete if self.public_content
    self.public_content = PublicContent.new(to_publish)
    self.save
  end

  def unpublish
    self.public_content.destroy
  end

  # OTU_PAGE_LAYOUTS
  #       V
  # OTU_PAGE_LAYOUT_SECTIONS ^ .otu_page_layout_id v .topic_id
  #       V
  #     TOPICS
  #       V
  #    CONTENTS              v otu_id              ^ .topic_id
  #       ^
  #      OTU
  #
  # Given an otu_page_layout id. find all the topics
  # For this otu_page_layout, find the topics (ControlledVocabularyTerm.of_type(:topic))

  def self.for_page_layout(otu_page_layout_id)
    where('topic_id in (?)', OtuPageLayout.where(id: otu_page_layout_id).first.topics.pluck(:id))
  end

  def self.find_for_autocomplete(params)
    where('text ILIKE ? OR text ILIKE ?', "#{params[:term]}%", "%#{params[:term]}%")
  end

  # @return [CSV]
  # Generate a version of the raw contents table for the given scope 
  # Ripped from http://railscasts.com/episodes/362-exporting-csv-and-excel
  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

end

Class Method Details

+ (Object) find_for_autocomplete(params)



83
84
85
# File 'app/models/content.rb', line 83

def self.find_for_autocomplete(params)
  where('text ILIKE ? OR text ILIKE ?', "#{params[:term]}%", "%#{params[:term]}%")
end

+ (Object) for_page_layout(otu_page_layout_id)

OTU_PAGE_LAYOUTS

V

OTU_PAGE_LAYOUT_SECTIONS ^ .otu_page_layout_id v .topic_id

   V
 TOPICS
   V
CONTENTS              v otu_id              ^ .topic_id
   ^
  OTU

Given an otu_page_layout id. find all the topics For this otu_page_layout, find the topics (ControlledVocabularyTerm.of_type(:topic))



79
80
81
# File 'app/models/content.rb', line 79

def self.for_page_layout(otu_page_layout_id)
  where('topic_id in (?)', OtuPageLayout.where(id: otu_page_layout_id).first.topics.pluck(:id))
end

+ (CSV) generate_download(scope)

Generate a version of the raw contents table for the given scope Ripped from railscasts.com/episodes/362-exporting-csv-and-excel

Returns:

  • (CSV)


90
91
92
93
94
95
96
97
98
99
# File 'app/models/content.rb', line 90

def self.generate_download(scope)
  CSV.generate do |csv|
    csv << column_names
    scope.order(id: :asc).each do |o|
      csv << o.attributes.values_at(*column_names).collect { |i|
        i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
      }
    end
  end
end

Instance Method Details

- (Object) publish

TODO: this will have to be updated in subclasses.



50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/content.rb', line 50

def publish
  to_publish = {
    topic: self.topic,
    text:  self.text,
    otu:   self.otu
  }

  self.public_content.delete if self.public_content
  self.public_content = PublicContent.new(to_publish)
  self.save
end

- (Boolean) published?

Returns true if this content has been published

Returns:

  • (Boolean)

    true if this content has been published



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

def published?
  self.public_content
end

- (Object) unpublish



62
63
64
# File 'app/models/content.rb', line 62

def unpublish
  self.public_content.destroy
end