Coder Social home page Coder Social logo

active_null's Introduction

Circle CI Coverage Status Code Climate

ActiveNull

Using Null Objects can make life a lot easier and avoid having excessive nil? checks. Avdi Grimm made an excellent library called Naught which makes working with and building Null Objects easier. Thank you Avdi for making this possible!

This library makes ActiveRecord Models aware of Null Objects and act as you would hope they would when using null objects in your project.

This is specially useful when combined with a decorator library like Draper. By knowing you have an object instead of the dreaded nil you can just decorate it and your view won't know any different.

Installation

Add this line to your application's Gemfile:

gem 'active_null'

And then execute:

$ bundle

Or install it yourself as:

$ gem install active_null

Usage

For each model you would like to be null aware, extend ActiveNull.

class Post < ActiveRecord::Base
  extend ActiveNull
  has_many :comments
end

class Comment < ActiveRecord::Base
  extend ActiveNull
  belongs_to :post
end

You can now do things like this:

Post.null              # => <null:Post>
Post.null.comments     # => <ActiveRecord::Relation []>
Comment.null           # => <null:Comment>
Comment.null.post      # => <null:Post>
Comment.new.post       # => <null:Post>
Post.find_by(id: 1)    # => <null:Post>

Additional Features

You may find you want your Null Object to respond to certain methods in a specific way. These methods might be new or they might be overrides of the default Null Object definition. You can define this in your model:

class Post < ActiveRecord::Base
  extend ActiveNull

  null_model do
    def some_method
      # special functionality
    end
  end
end

When using polymorphic relationships, ActiveNull must be told a default model to use for the Null Object representation.

class Post < ActiveRecord::Base
  extend ActiveNull
  belongs_to :author, polymorphic: true
  null_defaults_for_polymorphic author: 'User'
end

If you have Draper included in your project, the Null Object versions of your model will respond as expected to decorate.

You can then safely do something like:

@post = Post.find_by(id: 1).decorate

You can also define an alias for your null method for convenience

class User < ActiveRecord::Base
  extend ActiveNull

  null_model(:guest)
end

User.first    # => <User>
User.null     # => <null:User>
User.guest    # => <null:User>

Contributing

  1. Fork it ( https://github.com/Originate/active_null/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

active_null's People

Contributors

alexpeachey avatar danderozier avatar eturino avatar iachettifederico avatar kevgo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.