ActiveData is a ActiveModel-based frontend for your data. You might need to use it in the following cases:
- When you need a form objects pattern.
class ProfileForm
include ActiveData::Model
attribute 'first_name', type: String
attribute 'last_name', type: String
attribute 'birth_date', type: Date
def full_name
[first_name, last_name].reject(&:blank).join(' ')
end
def full_name= value
self.first_name, self.last_name = value.split(' ', 2).map(&:strip)
end
end
class ProfileController < ApplicationController
def edit
@form = ProfileForm.new current_user.attributes.slice(*%w(full_name birth_date))
end
def update
result = ProfileForm.new(params[:profile_form]).save do |form|
current_user.update_attributes(form.attributes.slice(*%w(full_name birth_date)))
end
if result
redirect_to ...
else
render 'edit'
end
end
end
- When you need to work with data-storage in ActiveRecord style with
class Flight
include ActiveData::Model
attribute :airline, type: String
attribute :number, type: String
attribute :departure, type: Time
attribute :arrival, type: Time
validates :airline, :number, presence: true
def id
[airline, number].join('-')
end
def self.find id
source = REDIS.get(id)
instantiate(JSON.parse(source)) if source.present?
end
define_save do
REDIS.set(id, attributes.to_json)
end
define_destroy do
REDIS.del(id)
end
end
- When you need to implement embedded objects for ActiveRecord models
class Answer
include ActiveData::Model
attribute :question_id, type: Integer
attribute :content, type: String
validates :question_id, :content, presence: true
end
class Quiz < ActiveRecord::Base
embeds_many :answers
validates :user_id, presence: true
validates :answers, associated: true
end
q = Quis.new
q.answers.build(question_id: 42, content: 'blabla')
q.save
Add this line to your application's Gemfile:
gem 'active_data'
And then execute:
$ bundle
Or install it yourself as:
$ gem install active_data
TODO: Write usage instructions here
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request