Code Monkey home page Code Monkey logo

acts_as_owner.rb's Introduction

Acts as owner


โš ๏ธ This gem is outdated. Please do not use it.


Acts as owner is a plugin for Ruby on Rails that provides to an owner object the ability to self-query about the possession of an ownable object.

Ownable objects can be any objects belonging to the owner object and any objects belonging to an ownable object.

Any ownable child, which belongs to an owned ownable parent, is also owned by the owner.

Status

Gem Version Build Status

Philosophy

General library that does only one thing, without any feature.

Installation

Include the gem in your Gemfile:

gem 'acts_as_owner'

And run the bundle command. Or as a plugin:

rails plugin install git://github.com/cyril/acts_as_owner.git

Getting started

Configuring models

Owner models just have to be declared thanks to acts_as_owner, with each ownable object passed through a block (using owns_one or owns_many method).

Example

class User < ActiveRecord::Base
  acts_as_owner do |u|
    u.owns_many :articles
    u.owns_many :comments
  end

  with_options(dependent: :destroy) do |opts|
    opts.has_many :articles
    opts.has_many :comments
  end
end

class Article < ActiveRecord::Base
  belongs_to :user
  has_many :comments, dependent: :destroy
end

class Comment < ActiveRecord::Base
  belongs_to :article
  belongs_to :user
end

# An article should be ownable by a user, so:
User.could_own_an?(:article)  # => true

# Considering this one:
article = current_user.articles.first

# We can see that:
current_user.owns? article    # => true

# Now, considering its first comment:
comment = article.comments.first

# Just like article, we can check that:
User.could_own_an? :comment   # => true

# Let's see if the current user is the author:
current_user == comment.user  # => false

# Never mind.  It belongs to his article so that's also his one:
current_user.owns? comment    # => true

Copyright (c) 2009-2011 Cyril Wack, released under the MIT license

acts_as_owner.rb's People

Contributors

cyril avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

nicolasva

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.