madebymany / sir-trevor-rails

Rails gem for Sir Trevor integration.
MIT License
149 stars 50 forks source link

Do not override default attribute getter with sir_trevor_content #51

Open zamakkat opened 8 years ago

zamakkat commented 8 years ago

I am not sure this is the default and intended behavior but would appreciate any help with this issue.

I have a simple Draft model with a content JSON column on it. On the front-end, I am using simple_form to generate a form and initialize the sir-trevor editor. With this setup everything works as it is supposed to. User edits on the editor, saves and JSON gets saved to the database. On refresh, the editor still loads the JSON and shows what the user previously saved.

However, when I add the sir_trevor_content :content from the gem, things stop working. Since that helper method overrides the default getter, using draft.content no longer returns the column values (the JSON) but returns an array of SirTrevorRails:Blocks. The only remaining way to actually get the JSON value to show in the editor is to use read_attribute which doesn't really look very nice.

I tried a workaround using alias_method and apply sir_trevor_content on the alias, still doesn't work. I also tried defining a custom method called content_view that returns the content, then pass that to sir_trevor_content but that doesn't work since in the concern, there is this line of code

content = read_attribute(attribute)

which restricts the use only to model attributes, not methods.

My current fix is simple although not very elegant. I just prefixed the method defined by sir_trevor_content with sir_trevor_ so that it doesn't override the default getter: https://github.com/zamakkat/sir-trevor-rails/commit/0a88182babf2d317e4bcec58b55997de6d014f0f Now, I can still use draft.content to freely edit the JSON using the sir-trevor editor and then display it by calling draft.sir_trevor_content to show it in the views.

Any other (better) workarounds and ways to fix the gem are welcomed and I can issue a PR if the owners are OK with this.

Cc: @shinnyx

lukaszsagol commented 8 years ago

In the latest version of sir_trevor_rails SirTrevorRails::BlockArray#to_s has been overwritten to return JSON representation of the array of blocks, which should make it work with form helpers.

Are you using the latest version of the gem (0.5.0)?