Skip to content

Commit

Permalink
add has_hoardable_rich_text for has_rich_text hoardable: true (#40)
Browse files Browse the repository at this point in the history
* add `has_hoardable_rich_text` for `has_rich_text hoardable: true`

* readme note

* formatting
  • Loading branch information
waymondo authored Jan 10, 2024
1 parent ea59504 commit 7575d55
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions .streerc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--print-width=100
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ Then in your model include `Hoardable::Model` and provide the `hoardable: true`
class Post < ActiveRecord::Base
include Hoardable::Model # or `Hoardable::Associations` if you don't need `PostVersion`
has_rich_text :content, hoardable: true
# alternately, this could be `has_hoardable_rich_text :content`
end
```

Expand Down
12 changes: 6 additions & 6 deletions lib/hoardable/has_rich_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ module HasRichText
extend ActiveSupport::Concern

class_methods do
def has_rich_text(name, encrypted: false, hoardable: false)
if SUPPORTS_ENCRYPTED_ACTION_TEXT
super(name, encrypted: encrypted)
else
super(name)
end
def has_rich_text(name, hoardable: false, **opts)
super(name, **opts)
return unless hoardable

reflection_options = reflections["rich_text_#{name}"].options
Expand All @@ -24,6 +20,10 @@ def has_rich_text(name, encrypted: false, hoardable: false)
"Hoardable"
)
end

def has_hoardable_rich_text(name, **opts)
has_rich_text(name, hoardable: true, **opts)
end
end
end
private_constant :HasRichText
Expand Down
2 changes: 2 additions & 0 deletions test/support/database.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

ActiveRecord::Schema.verbose = false

ActiveRecord::Schema.define do
create_table :posts, if_not_exists: true do |t|
t.text :body
Expand Down
2 changes: 2 additions & 0 deletions test/support/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class User < ActiveRecord::Base
class Profile < ActiveRecord::Base
include Hoardable::Model
belongs_to :user
has_hoardable_rich_text :life_story
has_hoardable_rich_text :diary, encrypted: true
end

class Comment < ActiveRecord::Base
Expand Down
19 changes: 19 additions & 0 deletions test/test_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,25 @@ def create_comments_and_destroy_post
end
end

test "has_hoardable_rich_text works" do
profile =
Profile.create!(user: user, email: "[email protected]", life_story: "<div>woke up</div>")
datetime = DateTime.now
profile.update!(life_story: "<div>went to sleep</div>")
assert_equal "woke up", profile.at(datetime).life_story.to_plain_text
end

if SUPPORTS_ENCRYPTED_ACTION_TEXT
test "has_hoardable_rich_text works for encrypted rich text" do
profile =
Profile.create!(user: user, email: "[email protected]", diary: "<div>i'm happy</div>")
datetime = DateTime.now
profile.update!(diary: "<div>i'm sad</div>")
assert_equal "i'm happy", profile.at(datetime).diary.to_plain_text
assert profile.diary.encrypted_attribute?("body")
end
end

test "returns correct polymoprhic association via temporal has one relationship" do
user = User.create!(name: "Joe Schmoe", bio: "<div>Bio</div>")
post = PostWithRichText.create!(title: "Title", content: "<div>Content</div>", user: user)
Expand Down

0 comments on commit 7575d55

Please sign in to comment.