Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not override default attribute getter with sir_trevor_content #51

Open
zamakkat opened this issue Apr 26, 2016 · 1 comment
Open

Comments

@zamakkat
Copy link

zamakkat commented Apr 26, 2016

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:
zamakkat@0a88182
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
Copy link
Contributor

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)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants