-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: if not set otherwise, identifiers are now generated from the sub…
…ject
- Loading branch information
Showing
6 changed files
with
79 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# a little maker for encapsulating entry creation behaviour. | ||
# orig. created to handle setting identifiers from subjects, but there's no | ||
# reason why in the near future this shouldn't handle _all_ of the entry callbacks | ||
# i.e. - set_identifier, set_subject, set_thread_identifier | ||
# but also even like, syncing to git and processing tags, contacts, links, etc. | ||
class EntryMaker | ||
def initialize(current_notebook) | ||
@current_notebook = current_notebook | ||
end | ||
|
||
def create(entry_params) | ||
Entry.transaction do | ||
@entry = @current_notebook.entries.find_by(identifier: entry_params[:identifier]) | ||
@entry ||= @current_notebook.entries.new(entry_params) | ||
|
||
# TODO: rename on update, but only if no other articles link to it yet. | ||
if @entry.generated_identifier? | ||
if @entry.set_subject.present? | ||
@entry.identifier = @entry.subject.parameterize | ||
end | ||
|
||
# TODO: need to restrict this up to 100 times or w/e | ||
while @current_notebook.entries.find_by(identifier: @entry.identifier) | ||
dash_number = @entry.identifier.match(/-\d+$/).to_s | ||
|
||
if dash_number.blank? | ||
@entry.identifier = "#{@entry.identifier}-1" | ||
else | ||
new_number = dash_number.gsub("-", "").to_i + 1 | ||
@entry.identifier = @entry.identifier.gsub(dash_number, "-#{new_number}") | ||
|
||
end | ||
end | ||
end | ||
|
||
return [@entry, !((@entry.new_record? && @entry.save) || @entry.update(entry_params))] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ class EntryTest < ActiveSupport::TestCase | |
@file_path = File.join(Rails.root, "test", "fixtures", "test_image.jpg") | ||
end | ||
|
||
# describe "subject & identifier are interrelated" do | ||
test "an identifier gets set by default, and it resembles a date with some random letters" do | ||
entry = @notebook.entries.new(body: "body") | ||
refute entry.identifier | ||
|
@@ -31,7 +30,6 @@ class EntryTest < ActiveSupport::TestCase | |
assert_equal "my subject", h2_subject.subject | ||
|
||
# if more than one heading is set it picks the first one | ||
|
||
h2h1_subject = @notebook.entries.create(body: "## first line\n# second line\nhi") | ||
|
||
assert_equal "first line", h2h1_subject.subject | ||
|
@@ -41,7 +39,6 @@ class EntryTest < ActiveSupport::TestCase | |
gasp_no_subject = @notebook.entries.create(body: "line1\n\nline2\b\nline3\n\n## this won't get read\n# neither will this\nhi") | ||
assert_nil gasp_no_subject.subject | ||
end | ||
# end | ||
|
||
test "#copy_parent" do | ||
parent_cal_entry = @notebook.entries.calendars.create(to: "[email protected], [email protected]", from: "[email protected]", body: "#test #right @foobar\n\nhello!\n\ntest") | ||
|