Class: Scriptorium::Widget::FeaturedPosts

Inherits:
ListWidget show all
Includes:
Contract
Defined in:
lib/scriptorium/widgets/featured_posts.rb

Constant Summary collapse

Title =
"Featured Posts"

Instance Attribute Summary

Attributes inherited from Scriptorium::Widget

#config, #name, #path, #repo, #view

Instance Method Summary collapse

Methods included from Contract

#assume, #check_invariants, enabled?, #invariant, #verify

Methods inherited from ListWidget

#load_data

Methods inherited from Scriptorium::Widget

#html_body, #html_card, #html_container

Methods included from Helpers

#cf_time, #change_config, #clean_slugify, #copy_gem_asset_to_user, #copy_to_clipboard, #d4, #escape_html, #generate_missing_asset_svg, #get_asset_path, #get_from_clipboard, #getvars, #list_gem_assets, #make_dir, #make_tree, #need, #read_commented_file, #read_file, #see, #see_file, #slugify, #substitute, #system!, #view_dir, #write_file, #write_file!, #ymdhms

Methods included from Exceptions

#make_exception

Constructor Details

#initialize(repo, view) ⇒ FeaturedPosts

Returns a new instance of FeaturedPosts.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/scriptorium/widgets/featured_posts.rb', line 12

def initialize(repo, view)
  assume { repo.is_a?(Scriptorium::Repo) }
  assume { view.is_a?(Scriptorium::View) }
  
  super(repo, view)
  @lines = @data
  
  define_invariants
  verify { @lines == @data }
  check_invariants
end

Instance Method Details

#cardObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/scriptorium/widgets/featured_posts.rb', line 51

def card
  check_invariants
  assume { @view.is_a?(Scriptorium::View) }
  assume { @name.is_a?(String) && !@name.empty? }
  
  file = "#{@view.dir}/widgets/#@name/#@name-card.html"
  result = read_file(file)
  
  verify { result.is_a?(String) }
  check_invariants
  result
end

#define_invariantsObject

Invariants



7
8
9
10
# File 'lib/scriptorium/widgets/featured_posts.rb', line 7

def define_invariants
  invariant { @lines.is_a?(Array) }
  invariant { Title.is_a?(String) && !Title.empty? }
end

#featured_post_item(post_id, list_title = nil) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/scriptorium/widgets/featured_posts.rb', line 111

def featured_post_item(post_id, list_title = nil)
  check_invariants
  assume { post_id.is_a?(String) && !post_id.empty? }
  assume { list_title.nil? || list_title.is_a?(String) }
  
  # Use actual post title from metadata, show error if post doesn't exist
  display_title = get_post_title(post_id)
  
  # Create link to the post (or error message)
  anchor = %[<a href="javascript:void(0)" onclick="load_main('posts/#{post_id}.html')" style="text-decoration: none;">#{display_title}</a>]
  result = %[<li class="list-group-item">#{anchor}</li>]
  
  verify { result.is_a?(String) && result.include?("list-group-item") }
  check_invariants
  result
end

#generateObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/scriptorium/widgets/featured_posts.rb', line 30

def generate
  check_invariants
  assume { @view.is_a?(Scriptorium::View) }
  
  write_main
  write_card
  result = true
  
  verify { result == true }
  check_invariants
  result
end

#get_post_title(post_id) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/scriptorium/widgets/featured_posts.rb', line 88

def get_post_title(post_id)
  check_invariants
  assume { post_id.is_a?(String) && !post_id.empty? }
  assume { @repo.is_a?(Scriptorium::Repo) }
  
  # Get the actual post title from metadata
  post = Scriptorium::Post.new(@repo, post_id)
  
  # Check if the post actually exists by looking for the meta file
  if File.exist?(post.meta_file)
    result = post.title || "Untitled Post"
  else
    result = "Error: Post #{post_id} not found"
  end
rescue => e
  # If post can't be created (invalid ID, etc.), return error message
  result = "Error: Post #{post_id} not found"
ensure
  verify { result.is_a?(String) && !result.empty? }
  check_invariants
  result
end

#load_configObject



24
25
26
27
28
# File 'lib/scriptorium/widgets/featured_posts.rb', line 24

def load_config
  check_invariants
  # No configuration needed for this widget
  check_invariants
end


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/scriptorium/widgets/featured_posts.rb', line 70

def parse_featured_line(line)
  check_invariants
  assume { line.is_a?(String) }
  
  # Parse line in format: <id> <title>
  # Title is optional, so we need to handle both cases
  parts = line.strip.split(/\s+/, 2)
  if parts.length >= 2
    result = [parts[0], parts[1]]
  else
    result = [parts[0], nil]
  end
  
  verify { result.is_a?(Array) && result.length == 2 }
  check_invariants
  result
end

#widget_titleObject



43
44
45
46
47
48
49
# File 'lib/scriptorium/widgets/featured_posts.rb', line 43

def widget_title
  check_invariants
  result = Title
  verify { result.is_a?(String) && !result.empty? }
  check_invariants
  result
end

#write_cardObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/scriptorium/widgets/featured_posts.rb', line 128

def write_card
  check_invariants
  assume { @view.is_a?(Scriptorium::View) }
  assume { @name.is_a?(String) && !@name.empty? }
  assume { @data.is_a?(Array) }
  
  tag = @name
  cardout = "#{@view.dir}/widgets/#@name/#@name-card.html"
  card_title = Title
  content = ""
  
  @data.each do |line|
    next if line.strip.empty?
    
    post_id, list_title = parse_featured_line(line)
    content << featured_post_item(post_id, list_title)
  end
  
  write_file(cardout, html_card(card_title, tag, content))
  check_invariants
end

#write_mainObject



64
65
66
67
68
# File 'lib/scriptorium/widgets/featured_posts.rb', line 64

def write_main
  check_invariants
  # Nothing in this case
  check_invariants
end