No commit activity in last 3 years
No release in over 3 years
Toggleable attributes for your ActiveRecord models.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 4.2.0.beta4
>= 4.2.0.beta4
 Project Readme

Acts As Toggleable

RubyGem Version Dependency Status Build Status Code Climate Code Coverage

Create toggleable attributes for your ActiveRecord models.

Why?

We frequently require the ability to toggle the visibility of content on an application's front-end.

Installation

gem install acts_as_toggleable

or in your Gemfile

gem 'acts_as_toggleable'

Usage

Make sure you require the library.

require 'acts_as_toggleable'

This gem follows a similar API to many ActiveRecord plugins:

class Widget < ActiveRecord::Base
  acts_as_toggleable :visible
  # acts_as_toggleable :visible, :default => false
end

This sets up a boolean field on the model, and also makes the following instance methods available:

widget = Widget.new
widget.visible
# => true

# toggle the widget's visibility, but don't persist the change
widget.toggle(:visible)
widget.visible
# => false

# toggle the widget's visibility and persist the change
widget.toggle!(:visible)

Options

In addition to the name of the attribute to create, you can also pass the following options:

  • :scope_name: The Symbol representing the name of the positive scope (optional, default: toggleable_attribute).
  • :inverse_scope_name: The Symbol representing the name of the negative scope (optional, default: "not_#{toggleable_attribute}").

Scopes

Two scopes are automatically created when you define a toggleable attribute:

class Widget < ActiveRecord::Base
  acts_as_toggleable :visible
end

widget1 = Widget.create(:visible => true)
widget2 = Widget.create(:visible => false)

Widget.visible
# => [widget1]

Widget.not_visible
# => [widget2]

These are just plain old ActiveRecord scopes, so you can chain them together with other scopes as needed:

Widget.visible.where(:created_at.lte => 10.days.ago)

You can also customize the names of the scope created:

class Widget < ActiveRecord::Base
  acts_as_toggleable :published, :inverse_scope_name => :unpublished
end

Widget.published
Widget.unpublished

Contributing to acts_as_toggleable

  • 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 we 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 we can cherry-pick around it.

Copyright

Copyright (c) 2016 PJ Kelly (Crush & Lovely). See LICENSE for further details.