Skip to content

Commit 9e902d3

Browse files
committed
WIP: if not set otherwise, identifiers are now generated from the subject
1 parent 0af932f commit 9e902d3

File tree

6 files changed

+79
-7
lines changed

6 files changed

+79
-7
lines changed

app/controllers/entries_controller.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ def edit
109109
# POST /entries.json
110110
# TODO: why do we support create or update in this method vs the method above?
111111
def create
112-
@entry = @current_notebook.entries.find_by(identifier: entry_params[:identifier])
113-
@entry ||= @current_notebook.entries.new(entry_params)
112+
maker = EntryMaker.new(current_notebook)
113+
@entry, err = maker.create(entry_params)
114114

115115
respond_to do |format|
116-
if (@entry.new_record? && @entry.save) || @entry.update(entry_params)
116+
if !err
117117
format.html do
118118
if request.referer =~ /agenda/
119119
redirect_back(fallback_location: timeline_path(current_notebook))
@@ -221,7 +221,7 @@ def find_or_build_entry
221221

222222
# Never trust parameters from the scary internet, only allow the white list through.
223223
def entry_params
224-
params.require(:entry).permit(:identifier, :body, :url, :subject, :occurred_at, :in_reply_to, :hide, :occurred_date, :occurred_time, files: [])
224+
params.require(:entry).permit(:identifier, :generated_identifier, :body, :url, :subject, :occurred_at, :in_reply_to, :hide, :occurred_date, :occurred_time, files: [])
225225
end
226226

227227
def bookmark_params

app/models/entry.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,15 @@ def copy_to!(notebook)
306306
notebook.entries.find_by(identifier: copy.identifier)
307307
end
308308

309+
## little hack for tracking whether the identifier value returned from the
310+
# form was set by the user, or was just the default generated identifier.
311+
# this allows me to decide whether to replace the identifier with the
312+
# parameterized subject
313+
attr_accessor :generated_identifier
314+
def generated_identifier?
315+
self.generated_identifier == self.identifier
316+
end
317+
309318
# this is actually pretty complicated to do properly?
310319
# https://github.com/middleman/middleman/blob/master/middleman-core/lib/middleman-core/util/data.rb
311320
# https://github.com/middleman/middleman/blob/master/middleman-core/lib/middleman-core/core_extensions/front_matter.rb

app/models/entry_maker.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# a little maker for encapsulating entry creation behaviour.
2+
# orig. created to handle setting identifiers from subjects, but there's no
3+
# reason why in the near future this shouldn't handle _all_ of the entry callbacks
4+
# i.e. - set_identifier, set_subject, set_thread_identifier
5+
# but also even like, syncing to git and processing tags, contacts, links, etc.
6+
class EntryMaker
7+
def initialize(current_notebook)
8+
@current_notebook = current_notebook
9+
end
10+
11+
def create(entry_params)
12+
Entry.transaction do
13+
@entry = @current_notebook.entries.find_by(identifier: entry_params[:identifier])
14+
@entry ||= @current_notebook.entries.new(entry_params)
15+
16+
# TODO: rename on update, but only if no other articles link to it yet.
17+
if @entry.generated_identifier?
18+
if @entry.set_subject.present?
19+
@entry.identifier = @entry.subject.parameterize
20+
end
21+
22+
# TODO: need to restrict this up to 100 times or w/e
23+
while @current_notebook.entries.find_by(identifier: @entry.identifier)
24+
dash_number = @entry.identifier.match(/-\d+$/).to_s
25+
26+
if dash_number.blank?
27+
@entry.identifier = "#{@entry.identifier}-1"
28+
else
29+
new_number = dash_number.gsub("-", "").to_i + 1
30+
@entry.identifier = @entry.identifier.gsub(dash_number, "-#{new_number}")
31+
32+
end
33+
end
34+
end
35+
36+
return [@entry, !((@entry.new_record? && @entry.save) || @entry.update(entry_params))]
37+
end
38+
end
39+
end

app/views/entries/_form.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
<%= form_with(model: entry, url: url, local: true) do |form| %>
2121
<%= form.hidden_field :identifier, value: @entry.set_identifier %>
22+
<%= form.hidden_field :generated_identifier, value: @entry.identifier %>
2223
<% if @parent_entry %>
2324
<%= form.hidden_field :in_reply_to, value: @parent_entry.identifier %>
2425
<% end %>

test/integration/entries_integration_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,30 @@ class EntriesIntegrationTest < ActionDispatch::IntegrationTest
8585
# put in Timecop right now.
8686
assert_equal textarea_text, "\n# #daily #{Date.today.to_s}"
8787
end
88+
89+
test "when saved thru a controller, an entry's identifier will be set from its subject" do
90+
assert_equal 0, @current_notebook.entries.count
91+
post create_entry_path(owner: @current_notebook.owner, notebook: @current_notebook), params: { entry: { body: "test mc test"} }
92+
93+
# ideally look this up from the redirect path or w/e
94+
# here we can count on the most recent one having been just created
95+
entry_with_no_subject = @current_notebook.entries.last
96+
97+
# no subject, no identifier tweak
98+
assert_nil entry_with_no_subject.subject
99+
# same as defined in entry_test.rb
100+
assert entry_with_no_subject.identifier =~ /\d\d\d\d\d\d\d\d-[23456789cfghjmpqrvwx]{4}/
101+
102+
# with a subject it should set the identifier
103+
post create_entry_path(owner: @current_notebook.owner, notebook: @current_notebook), params: { entry: { body: "# has a subject\n\ntest mc test"} }
104+
105+
entry_with_subject = @current_notebook.entries.last
106+
107+
# TODO:
108+
# - create with generated_identifier
109+
# - it replaces it with the subject
110+
# - but not if the identifier was modified
111+
# - then it sets the identifier value
112+
# - also if the post already exists it will increment the -1 on the identifier
113+
end
88114
end

test/models/entry_test.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ class EntryTest < ActiveSupport::TestCase
66
@file_path = File.join(Rails.root, "test", "fixtures", "test_image.jpg")
77
end
88

9-
# describe "subject & identifier are interrelated" do
109
test "an identifier gets set by default, and it resembles a date with some random letters" do
1110
entry = @notebook.entries.new(body: "body")
1211
refute entry.identifier
@@ -31,7 +30,6 @@ class EntryTest < ActiveSupport::TestCase
3130
assert_equal "my subject", h2_subject.subject
3231

3332
# if more than one heading is set it picks the first one
34-
3533
h2h1_subject = @notebook.entries.create(body: "## first line\n# second line\nhi")
3634

3735
assert_equal "first line", h2h1_subject.subject
@@ -41,7 +39,6 @@ class EntryTest < ActiveSupport::TestCase
4139
gasp_no_subject = @notebook.entries.create(body: "line1\n\nline2\b\nline3\n\n## this won't get read\n# neither will this\nhi")
4240
assert_nil gasp_no_subject.subject
4341
end
44-
# end
4542

4643
test "#copy_parent" do
4744
parent_cal_entry = @notebook.entries.calendars.create(to: "[email protected], [email protected]", from: "[email protected]", body: "#test #right @foobar\n\nhello!\n\ntest")

0 commit comments

Comments
 (0)