Nawl is a gem that adds custom null objects to your Rails models. These null objects are perfect for encapsulating the logic in the case of a missing record, such as a post's missing author or a product's missing image.
Add this line to your application's Gemfile:
gem 'nawl'
And then execute:
$ bundle
Or install it yourself as:
$ gem install nawl
A quick usage example:
class Post
include Nawl
has_null_object title: 'Untitled'
end
Post.new.title # => nil
Post.new.author_name # => nil
Post.null_object.title # => 'Untitled'
Post.null_object.author_name # => nil
Post.null_object.class # => Post
A simple object generator is included, for those special cases where you want to represent nested objects in your null object:
class Photo
has_atached_file :image
has_one :post
end
class Post
belongs_to :photo
include Nawl
has_null_object image: Nawl.chain(
'photo.image.url',
'http://placekitten.com/g/200/200'
)
end
Post.null_object.photo.image.url
# => 'http://placekitten.com/g/200/200'
Post.null_object.photo.image.foo_bar
# => nil
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request