month_constrain is for managing columns of Date type as month information in ActiveRecord.
Converts date information such as Date.new(2016, 12, 12)
and "2016-01"
to Date Class.
Supports Rails 4.x and 5.x releases.
Add this line to your application's Gemfile:
gem 'month_constrain'
And then execute:
$ bundle
Or install it yourself as:
$ gem install month_constrain
In your model
class Book < ApplicationRecord
acts_as_month_constrain :published_on
# ...
end
Create Record
e.g.
>> Client.create(published_on: '2016-01')
# => #<Client id: 1, published_on: "2016-01-01">
>> Client.create(published_on: Date.new(2016, 1, 16))
# => #<Client id: 1, published_on: "2016-01-01">
Find Record
e.g.
>> Client.published_on('2016-01')
# => #<ActiveRecord::Relation [#<Client id: 1, published_on: "2016-01-01">]>
>> Client.published_on(Date.new(2016, 1, 16))
# => #<ActiveRecord::Relation [#<Client id: 1, published_on: "2016-01-01">]>
>> Client.published_on_in(Date.new(2016, 1, 16), nil)
# => #<ActiveRecord::Relation [#<Client id: 5, published_on: "2016-01-01">, #<Client id: 6, published_on: "2016-02-01">, #<Client id: 7, published_on: "2016-03-01">]>
>> Client.published_on_in('2016-01', '2016-02')
# => #<ActiveRecord::Relation [#<Client id: 5, published_on: "2016-01-01">, #<Client id: 6, published_on: "2016-02-01">]>
- Fork it ( https://github.com/speee/month_constrain/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
The gem is available as open source under the terms of the MIT License.