-
Notifications
You must be signed in to change notification settings - Fork 40
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
Customizable :locked_at and :locked_until fields #55
Conversation
b54d954
to
f19d223
Compare
Every time travis failures one of job buildings. |
For rubocop in General we do |
I like the spirit of where this feature is going, however I would expect to be able to configure the field names "normally" and not through defining constants. Mongoid::History.configure do |config|
config.locked_at_field = :mongoid_locker_locked_at
config.locked_until_field = :mongoid_locker_locked_until
end And possibly per-class class Foobar
include Mongoid::Locker
locker locked_at_field: :mongoid_locker_locked_at
end If you need a reference of how to structure such configuration check out https://github.com/mongoid/mongoid-slug |
a1ca391
to
990fa22
Compare
I am afraid to use auto correction I have chosen this way: class Foobar
include Mongoid::Locker
locker locked_at_field: :mongoid_locker_locked_at
end |
This seems to now require you call There's no way to set this globally, which seems like a big deal for most people. I think you should implement a global config on top of this to resolve both this and the first issue. |
Just question.
class Foobar
include Mongoid::Locker
locker
# or the same
locker locked_at_field: :mongoid_locker_locked_at
end
Mongoid::History.configure do |config|
config.locked_at_field = :mongoid_locker_locked_at
config.locked_until_field = :mongoid_locker_locked_until
end
class Foobar
include Mongoid::Locker
# it is already configured by default
end This is achieved within def self.included(mod)
mod.field :locked_at, type: Time
end
Mongoid::History.configure do |config|
config.locked_at_field = :locked_at
config.locked_until_field = :locked_until
end
class Foobar
include Mongoid::Locker
# it is already configured by default
# field :locked_at, type: Time
locker locked_at_field: :mongoid_locker_locked_at
# field :mongoid_locker_locked_at
end What we should do with already defined field We get some side effect or at least the warn: WARN -- : Overwriting existing field locked_at in class Foobar. In my first implementation I declared custom field through constant. It allowed us don't change any code in user application, just defined the constant in model where it really needed like |
I think I understand the problem. This is something we faced in other gems, especially mongoid-history. The inclusion of the module currently defines the field and then a a) Raise an exception if I think a) is fine and resolved a bunch of these problems. What do you think? |
I think field definition should be extracted into model. User should be allowed define field in the model. In this way we avoid hard coded Yes. These changes will affect users which already used the gem. They should add only one line By default: class User
include Mongoid:Locker
field :locked_at, type: Time
end Global configure Mongoid::Locker.configure do |c|
c.locked_at_field = :global_locker_locked_at
end
class User
include Mongoid:Locker
field :locked_at, type: Time # for devise
field :global_locker_locked_at, type: Time # for locker
end With Mongoid::Locker.configure do |c|
c.locked_at_field = :global_locked_at
end
class User
include Mongoid:Locker
field :locked_at, type: Time # for devise
field :locker_locked_at, type: Time # for locker
locker locked_at_field: :locker_locked_at
end |
I am ok with it. Curious what @afeld thinks too (the original author). ' Maybe Implement the above and add an UPGRADING doc ala https://github.com/mongoid/mongoid-history/blob/master/UPGRADING.md that describes what users have to do to this PR, bump major version? |
I will try implement.
I will add test for this: it 'should return error if locked_at field is not defined' do
Admin.field(:locked_until, type: Time)
admin = Admin.create!
expect do
admin.with_lock do
# no-op
end
end.to raise_error(Mongoid::Errors::UnknownAttribute)
end
it 'should return error if locked_until field is not defined' do
Admin.field(:locked_at, type: Time)
admin = Admin.create!
expect do
admin.with_lock do
# no-op
end
end.to raise_error(Mongoid::Errors::UnknownAttribute)
end Should we catch something special or it is enough? |
8f2860e
to
ea78f72
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great code-wise.
I made some suggestions to cleanup docs, please make them.
There seems to be a bit of deletion going on in mongoid-locker_spec.rb. Are we losing tests? :)
CHANGELOG.md
Outdated
|
||
* Your contribution here. | ||
|
||
### 1.0.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This hasn't been released so just make it 1.0.0 (Next).
README.md
Outdated
end | ||
``` | ||
|
||
`locker` method in your model accepts `:locked_at_field` and `:locked_until_field` options to setup field names which used by Locker for the model. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The locker
method ..
maybe add
This can be useful when another popular library uses the same field for different purposes.
README.md
Outdated
end | ||
``` | ||
|
||
This may be useful to avoid clashing with [Devise](https://github.com/plataformatec/devise) when it uses the same `:locked_at` field (see [Issue #26](https://github.com/mongoid/mongoid-locker/issues/26)). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would remove this paragraph, we don't like to point fingers at other small unpopular libraries who decide to call fields the same name :)
UPGRADING.md
Outdated
## Upgrading Mongoid Locker | ||
|
||
### Upgrading to 1.0.0 | ||
Locker is now doesn't define `:locked_at` and `:locked_until` fields by default during include. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mongoid::Locker
no longer defines locked_at
and locked_until
fields when included. You must define these fields manually.
UPGRADING.md
Outdated
end | ||
``` | ||
|
||
Field names used by Locker can be defined with `locker` class method or through global `configure`. See [Customizable :locked_at and :locked_until field names](https://github.com/mongoid/mongoid-locker#customizable-locked_at-and-locked_until-field-names) for more information. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can customize the fields used with a locker
class method or via a global configure
.
UPGRADING.md
Outdated
|
||
Field names used by Locker can be defined with `locker` class method or through global `configure`. See [Customizable :locked_at and :locked_until field names](https://github.com/mongoid/mongoid-locker#customizable-locked_at-and-locked_until-field-names) for more information. | ||
|
||
[#55](https://github.com/mongoid/mongoid-locker/pull/55): Customizable :locked_at and :locked_until fields. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See ...
No. |
I am cool with refactoring tests btw! |
I've merged this, excellent work. @dks17 Do you want to help out with maintaining this gem? Make a release? If yes what's your rubygems email? |
Thank you for the assist. |
@dblock, yes, I would like to try help out with maintaining this gem. |
Added you to collaborators (https://github.com/mongoid/mongoid-locker/invitations), added to Rubygems. Thanks. Maybe check out #56 |
2
and3
versions on my pc.Reference #26