diff --git a/.streerc b/.streerc new file mode 100644 index 0000000..f8bee31 --- /dev/null +++ b/.streerc @@ -0,0 +1 @@ +--print-width=100 diff --git a/README.md b/README.md index ca7f7de..550aa51 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/lib/hoardable/has_rich_text.rb b/lib/hoardable/has_rich_text.rb index 6ac299f..4fc4eed 100644 --- a/lib/hoardable/has_rich_text.rb +++ b/lib/hoardable/has_rich_text.rb @@ -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 @@ -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 diff --git a/test/support/database.rb b/test/support/database.rb index 69a487d..448dc88 100644 --- a/test/support/database.rb +++ b/test/support/database.rb @@ -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 diff --git a/test/support/models.rb b/test/support/models.rb index 7db1113..66b86ad 100644 --- a/test/support/models.rb +++ b/test/support/models.rb @@ -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 diff --git a/test/test_model.rb b/test/test_model.rb index ed51d15..2b51d02 100644 --- a/test/test_model.rb +++ b/test/test_model.rb @@ -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@example.com", life_story: "
woke up
") + datetime = DateTime.now + profile.update!(life_story: "
went to sleep
") + 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@example.com", diary: "
i'm happy
") + datetime = DateTime.now + profile.update!(diary: "
i'm sad
") + 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: "
Bio
") post = PostWithRichText.create!(title: "Title", content: "
Content
", user: user)