Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rosylilly/active_strategy

Extract the business logic from the model
https://github.com/rosylilly/active_strategy

Last synced: 5 days ago
JSON representation

Extract the business logic from the model

Awesome Lists containing this project

README

        

# ActiveStrategy

[![Build Status](https://travis-ci.org/rosylilly/active_strategy.png?branch=master)](https://travis-ci.org/rosylilly/active_strategy)
[![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/rosylilly/active_strategy)
[![Dependency Status](https://gemnasium.com/rosylilly/active_strategy.png)](https://gemnasium.com/rosylilly/active_strategy)

Extract the business logic from the model.

# Usage

1. bundle `active_strategy` gem
2. create a strategy class. For example, a strategy for a `login` method should be named `LoginStrategy`. You can use the generator for doing this (`$ rails generate strategy Login`)

# Examples

```ruby
# app/models/user.rb
class User < ActiveRecord::Base
include ActiveStrategy::Context

strategy :login, class_method: true

attr_accessible :email, :password
end

# app/strategies/login_strategy.rb
class LoginStrategy
include ActiveStrategy::Strategy

def login(email, password)
user = @class.where(:email => email).first

return user.try(:password) == password ? user : nil
end
end

# app/controllers/authenticate_controller.rb
class AuthenticateController < ApplicationControlelr
def login
session[:user_id] = User.login(params[:email], params[:password])
end
end
```

# Copyright

Copyright (c) 2012 Sho Kusano. See MIT-LICENSE for further details.