lazy_model_const¶ ↑
A convenient variant of lazy_const
with an eye towards ActiveRecord models.
Installation¶ ↑
gem 'lazy_model_const'
Example¶ ↑
Here’s an example using Rails. Really, any class that has a class method find_or_create_by_name
can be used. If you’re not using ActiveRecord, you probably want to use the lazy_const
gem.
app/model/group.rb:
class Group < ActiveRecord::Base extend LazyConst lazy_const :EVERYONE, name: 'Everyone' end
app/model/user.rb:
class User < ActiveRecord::Base has_many :groups after_create :add_to_everyone def add_to_everyone groups << Group.EVERYONE end end
RSpec isn’t required, but if you happen to use it you can use the helper.
spec/spec_helper.rb:
require 'lazy_const/spec'
spec/model/group_spec.rb:
describe Group do defines_model_const :EVERYONE # ... end
Contributing to lazy_model_const¶ ↑
-
Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet.
-
Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it.
-
Fork the project.
-
Start a feature/bugfix branch.
-
Commit and push until you are happy with your contribution.
-
Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
Copyright¶ ↑
Copyright © 2013 Ryan Graham. See LICENSE.txt for further details. (TL;DR: MIT license)