Class: ContentBlock

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

Constant Summary collapse

NAME_REGISTRY =

The keys in this registry are “public” names for collaborator objects, and the values are reserved names of ContentBlock instances, which Hyrax uses as identifiers. Values also correspond to names of messages that can be sent to the ContentBlock class to return defined ContentBlock instances.

{
  marketing: :marketing_text,
  researcher: :featured_researcher,
  announcement: :announcement_text,
  about: :about_page,
  help: :help_page,
  terms: :terms_page,
  agreement: :agreement_page
}.freeze

Class Method Summary collapse

Class Method Details

.about_pageObject



55
56
57
# File 'app/models/content_block.rb', line 55

def about_page
  find_or_create_by(name: 'about_page')
end

.about_page=(value) ⇒ Object



59
60
61
# File 'app/models/content_block.rb', line 59

def about_page=(value)
  about_page.update(value: value)
end

.agreement_pageObject



63
64
65
66
# File 'app/models/content_block.rb', line 63

def agreement_page
  find_by(name: 'agreement_page') ||
    create(name: 'agreement_page', value: default_agreement_text)
end

.agreement_page=(value) ⇒ Object



68
69
70
# File 'app/models/content_block.rb', line 68

def agreement_page=(value)
  agreement_page.update(value: value)
end

.announcement_textObject



39
40
41
# File 'app/models/content_block.rb', line 39

def announcement_text
  find_or_create_by(name: 'announcement_text')
end

.announcement_text=(value) ⇒ Object



43
44
45
# File 'app/models/content_block.rb', line 43

def announcement_text=(value)
  announcement_text.update(value: value)
end

.default_agreement_textObject



89
90
91
92
93
94
95
# File 'app/models/content_block.rb', line 89

def default_agreement_text
  ERB.new(
    IO.read(
      Hyrax::Engine.root.join('app', 'views', 'hyrax', 'content_blocks', 'templates', 'agreement.html.erb')
    )
  ).result
end

.default_terms_textObject



97
98
99
100
101
102
103
# File 'app/models/content_block.rb', line 97

def default_terms_text
  ERB.new(
    IO.read(
      Hyrax::Engine.root.join('app', 'views', 'hyrax', 'content_blocks', 'templates', 'terms.html.erb')
    )
  ).result
end


47
48
49
# File 'app/models/content_block.rb', line 47

def featured_researcher
  find_or_create_by(name: 'featured_researcher')
end


51
52
53
# File 'app/models/content_block.rb', line 51

def featured_researcher=(value)
  featured_researcher.update(value: value)
end

.for(key) ⇒ Object

NOTE: method defined outside the metaclass wrapper below because ‘for` is a reserved word in Ruby.

Raises:

  • (ArgumentError)


20
21
22
23
24
# File 'app/models/content_block.rb', line 20

def self.for(key)
  key = key.respond_to?(:to_sym) ? key.to_sym : key
  raise ArgumentError, "#{key} is not a ContentBlock name" unless registered?(key)
  ContentBlock.public_send(NAME_REGISTRY[key])
end

.help_pageObject



72
73
74
# File 'app/models/content_block.rb', line 72

def help_page
  find_or_create_by(name: 'help_page')
end

.help_page=(value) ⇒ Object



76
77
78
# File 'app/models/content_block.rb', line 76

def help_page=(value)
  help_page.update(value: value)
end

.marketing_textObject



31
32
33
# File 'app/models/content_block.rb', line 31

def marketing_text
  find_or_create_by(name: 'marketing_text')
end

.marketing_text=(value) ⇒ Object



35
36
37
# File 'app/models/content_block.rb', line 35

def marketing_text=(value)
  marketing_text.update(value: value)
end

.registered?(key) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'app/models/content_block.rb', line 27

def registered?(key)
  NAME_REGISTRY.include?(key)
end

.terms_pageObject



80
81
82
83
# File 'app/models/content_block.rb', line 80

def terms_page
  find_by(name: 'terms_page') ||
    create(name: 'terms_page', value: default_terms_text)
end

.terms_page=(value) ⇒ Object



85
86
87
# File 'app/models/content_block.rb', line 85

def terms_page=(value)
  terms_page.update(value: value)
end