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

Remove Mobility::Interface #229

Merged
merged 1 commit into from
May 23, 2018
Merged

Remove Mobility::Interface #229

merged 1 commit into from
May 23, 2018

Conversation

shioyama
Copy link
Owner

@shioyama shioyama commented May 22, 2018

Mobility currently has an interface for accessing certain model-global methods like names which returns the attribute names on the model (for all backends). This is accessible via Post.mobility.

However, with the extensive changes in #225, I'm finding that having both the class query scope Post.i18n and the interface on Post.mobility is getting pretty clumsy. Here I am going to try to move this interface stuff into the Attributes module builder so that class methods are defined on the model as soon as any attributes are defined, without using a memoized instance variable (which requires extra precautions when duping etc.)

The goal here is to have this type of interface for accessors and backends:

class Post < ApplicationRecord
  extend Mobility
  translates :title
end

post = Post.new
post.mobility_backends
#=> {}
post.title = "foo"
post.mobility_backends
#=> {:title=> <memoized instance of backend class>}
Post.mobility_backend_class(:title)
#=> #<Mobility::Backends::ActiveRecord::...>

In the i18n scope, you also get the [] shortcut to access nodes:

Post.i18n[:title]
#=> Arel node for `title` attribute

Although I made querying into a plugin (on the master branch only), I'm going to convert it into a normal module which is included by default when you extend Mobility. Querying in the end is something global that should only enabled on an entire model, or not at all.

@shioyama shioyama force-pushed the remove_interface branch 2 times, most recently from beb8417 to e5bacca Compare May 22, 2018 02:38
@shioyama shioyama mentioned this pull request May 22, 2018
@shioyama shioyama force-pushed the remove_interface branch 2 times, most recently from 52c35e8 to b037b9b Compare May 22, 2018 14:04
@shioyama
Copy link
Owner Author

shioyama commented May 22, 2018

I've gone with backend_node as the name of the method to get the node on the query scope:

Post.i18n.backend_node(:title)
#=> Arel node for title attribute

Anything generic like [] will possibly conflict with other scope methods added by gems, etc.

@shioyama shioyama force-pushed the remove_interface branch from b037b9b to 6f99431 Compare May 22, 2018 23:49
@shioyama shioyama merged commit 6f99431 into master May 23, 2018
@shioyama shioyama deleted the remove_interface branch May 25, 2018 01:27
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

Successfully merging this pull request may close these issues.

1 participant