Skip to content
This repository

Rails 3.x gem for moderating ActiveRecord models (like posts, attributes...).

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 Gemfile
Octocat-spinner-32 Gemfile.lock
Octocat-spinner-32 MIT-LICENSE
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 Rakefile
Octocat-spinner-32 has_moderated.gemspec
README.rdoc

HasModerated

Installing

gem install has_moderated

And add it to your project's Gemfile

gem "has_moderated"

To set up has_moderated in your project, use

rails generate has_moderated:install

rake db:migrate

This will generate a Moderation model and a migration for it.

Upgrading

When upgrading, rerun the generator

rails generate has_moderated:install

If there is a new migration file and you have filename conflicts, remove the old one and apply the new one, in case the schema changed.

Usage

To moderate one or more attributes, add

has_moderated :attr1, :attr2

to your model.

If you want to moderate the creation of a record (for example, you don't want new blog posts to show up until they are accepted by a moderator), use

has_moderated_create

You can also specify associations that need to be saved for moderation as well (if moderating the creation of new records) - for example, if a Post has_many :links, and you want to submit these links to moderation as well (note, if you don't, they will be discarded), use

has_moderated_create :with_associations => [:links]

in your Post model (post.rb). This only matters when you use has_moderated_create. You can also use

has_moderated_create :with_associations => :all

to include all associations, but I recommend you explicitly specify them if possible. By default, no associations are included.

To moderate destruction of records, use

has_moderated_destroy

Moderating associations on existing records

Sometimes you will want to moderate association changes for existing records, not only on create. Right now, there is only one way to do this and it's not as automagic as most of the other things. Instead of doing something like

post.comments << new_comment
post.scores << new_score

You will instead have to call add_associations_moderated like so

post.add_associations_moderated(:comments => [new_comment], :scores => [new_score])

The values can be either new records (not in the database), existing records, or Fixnum (numerical) IDs. Please note you should not use .build to create new records, because if you call save on the parent model it will automatically create the record, bypassing moderation.

Manage moderations

To see pending moderations, simply call

Moderation.all

You can also see moderations for a specific record. For example, if you have Post model, you can call moderations on it.

post = Post.first
post.moderations

Moderation is a normal ActiveRecord model, you can inspect it in rails console to see what it holds. Data (attr_value) is serialized in YAML format and can be deserialized by calling

YAML::load(moderation.attr_value)

To accept a moderation, call

moderation.accept

to discard (destroy) it, call

moderation.discard

Tests

I've tested this project using RSpec. You can find the tests in

test/dummy/spec/models/task_spec.rb

You can run the tests by running

rake spec

in the root directory.

TODO

Amend moderations… Eg if you create a new record and save it, then change something additionally and save again.

Problems

You may encounter problems with models that have some sort of non-serializable attributes. This might be something like file attachments, you'll have to try it to see. If you have a problem like that you can extract the problematic attributes into a seperate has_one association. If you moderate create, save that model without the foreign key first, and then use has_moderated_create :with_associations => [:association_name] and add the association (to the existing associated model) before saving the moderated model. If you have questions about this or don't understand what I mean, open an issue here at GitHub and I will explain it further.

License

This project rocks and uses MIT-LICENSE.

Something went wrong with that request. Please try again.