Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Mongoid Reform
Ruby
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
spec
.gitignore
.rspec
.ruby-version
Gemfile
Gemfile.lock
README.md
Rakefile
deform.gemspec

README.md

Deform

Reform for Mongoid

Validations inside model are sucks! Fat models are sucks! You have no control what and when validations are checking using traditional way and fat models are unreadable!

This way you can create simple and readable class that can validate parameters in one specific context.

Instalation

gem 'deform', git: 'git@github.com:Eraden/deform.git'

Usage

Methods

#property :field, on: :target

#attach :target, to: :target

#order :first_target, :second_target

#model 'Name' # create ActiveModel::Name

Example Documents

class User
  has_one :profile
  # fields...
end

class Profile
  belongs_to :user
  # fields...
end

Example Form

class RegisterForm < Deform::Base
  property :login, on: :user
  property :email, on: :user
  property :password, on: :user
  property :first_name, on: :profile
  property :last_name, on: :profile

  attach :user, to: :profile # You can pass object to other object
  attach :profile, to: :user # optional in second way

  order :user, :profile # optional

  validates :login, :email, presence: true

  model 'User'
end

Example Controller

class UserController < ApplicationController
  def new
    @form = RegisterForm.new user: User.new, profile: Profile.new # user will be pushed to profile
  end

  def create
    @form = RegisterForm.new user: User.new, profile: Profile.new
    if @form.submit params[:user]
      render json: { user: @form.user }
    else
      render json: { user: @form.errors }
    end
  end
end

Example Service

class RegisterService
  def create(user)
    user.password = encrypt(user.password)
    user.save
    user.profile.save
  end

  def encrypt(str)
    # ...
  end
end

Example service usage

class RegisterForm < Deform::Base
  # ...
  service using: RegisterService, pass: :user
  # ...
end

Test dependencies

sudo su

apt-get install phantomjs

cd /usr/src

wget https://phantomjs.googlecode.com/files/phantomjs-1.9.1-linux-x86_64.tar.bz2  -O phantomjs.tar.bz2

tar -xvf phantomjs.tar.bz2

cp bin/phantomjs /usr/bin/phantomjs

rm -Rf phantomjs phantomjs.tar.bz2

apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10

echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/10gen.list

apt-get update

apt-get purge mongodb # Important!

apt-get install mongodb-10gen

exit

Do not compile. This takes 30 minutes and many times though file not found execptions.

Why upgrade MongoDB

Because old one have many memory leaks, haven't many methods such as fetch and sucks!

Why poltergeist with phantomjs?

Because it's quickest, can create screenshots and doesn't need X!

Something went wrong with that request. Please try again.