Code Monkey home page Code Monkey logo

activerecord-tableless's Introduction

ActiveRecord Tableless

Build Status Dependency Status Code Climate

A single implementation of the ActiveRecord Tableless pattern for any Rails project or other Ruby project that uses ActiveRecord.

Why, why, why

Why would you ever consider this gem as opposed to ActiveModel.

In Rails 2 ActiveModel does not exists (this is when it all started; see History section)

In Rails 3 there is an Active Model API, where you can use some features of Active Record in other classes. Yehuda Katz has written a nice introduction about this.

In Rails 4 basic features of the Active Model API can be activated by including ActiveModel:Model in the model. Carlos Antônio has written a nice tutorial about this.

However neither the Active Model API (Rails 3) nor the ActiveModel::Model (Rails 4) supports relations and nested attributes.

Installation

ActiveRecord Tableless is distributed as a gem, which is how it should be used in your app.

Include the gem in your Gemfile:

gem "activerecord-tableless", "~> 1.0"

Supported Versions

Supported version are ActiveRecord version 2.3.x, 3.0.x series, 3.2.x series and 4.x series

You may be able to make it work with 3.1.x, but you should expect to put some time in it.

Usage

Define a model like this:

class ContactMessage < ActiveRecord::Base
  has_no_table
  column :name, :string
  column :email, :string
  validates_presence_of :name, :email
end

You can now use the model in a view like this:

<%= form_for :message, @message do |f| %>
  Your name: <%= f.text_field :name %>
  Your email: <%= f.text_field :email %>
<% end %>

And in the controller:

def message
  @message = ContactMessage.new
  if request.post?
    @message.attributes = params[:message]
    if @message.valid?
      # Process the message...
    end
  end
end

For Rails 2.3.x series you need to add this line in the top of your model file.

require 'activerecord-tableless'

If you wish (this is not recommended), you can pretend you have a succeeding database by using

has_no_table :database => :pretend_success

Development

To start develop, please download the source code

git clone git://github.com/softace/activerecord-tableless.git

When downloaded, you can start issuing the commands like

bundle install
bundle exec rake appraisal:gemfiles
bundle exec rake appraisal:install
bundle exec rake appraisal

Or you can see what other options are there:

bundle exec rake -T

History

Originally this code was implemented for Rails 2 by Kenneth Kalmer. For Rails 3 the need for this functionality was reduced dramatically due to the introduction of ActiveModel. But because the ActiveModel does not support relations and nested attributes the existence of this gem is still justified.

For a history of technical implementation details feel free to take a look in the git log :-)

Copyright

Copyright (c) Jarl Friis. See LICENSE.txt for further details.

activerecord-tableless's People

Contributors

brendon avatar davefp avatar jarl-dk avatar johanneswuerbach avatar kennethkalmer avatar kreintjes avatar skamithi avatar wagenet avatar

Watchers

 avatar  avatar

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.