Permalink
Browse files

First commit - bases of crushyform schema

  • Loading branch information...
0 parents commit c7623a0a042f35198dfaef0feb5845a61991f37f @mig-hub committed May 3, 2011
Showing with 154 additions and 0 deletions.
  1. +19 −0 LICENSE
  2. +3 −0 README.md
  3. +30 −0 lib/sequel_crushyform.rb
  4. +12 −0 sequel-crushyform.gemspec
  5. +90 −0 test/spec_crushyform.rb
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2011 Mickael Riga
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
@@ -0,0 +1,3 @@
+CRUSHYFORM
+----------
+
@@ -0,0 +1,30 @@
+module ::Sequel::Plugins::Crushyform
+
+ module ClassMethods
+ def crushyform_schema
+ @crushyform_schema ||= default_crushyform_schema
+ end
+ def crushyform_schema=(h); @crushyform=h; end
+ def default_crushyform_schema
+ out = {}
+ db_schema.each do |k,v|
+ out[k] = if v[:db_type]=='text'
+ {:type=>:text}
+ else
+ {:type=>v[:type]}
+ end
+ end
+ @schema.columns.each{|c|out[c[:name]]=out[c[:name]].update(c[:crushyform]) if c.has_key?(:crushyform)} if respond_to?(:schema)
+ association_reflections.each{|k,v|out[v[:key]]={:type=>:parent} if v[:type]==:many_to_one}
+ out
+ end
+ end
+
+ module InstanceMethods
+ def crushyfield(col, type=nil)
+ col = col.to_sym
+ type ||= self.class.crushyform_schema[col]
+ end
+ end
+
+end
@@ -0,0 +1,12 @@
+Gem::Specification.new do |s|
+ s.name = 'sequel-crushyform'
+ s.version = "0.0.1"
+ s.platform = Gem::Platform::RUBY
+ s.summary = "A Sequel plugin that helps building forms"
+ s.description = "A Sequel plugin that helps building forms. It basically does them for you so that you can forget about the boring part."
+ s.files = `git ls-files`.split("\n").sort
+ s.require_path = './lib'
+ s.author = "Mickael Riga"
+ s.email = "mig@mypeplum.com"
+ s.homepage = "http://github.com/mig-hub/sequel-crushyform"
+end
@@ -0,0 +1,90 @@
+require 'rubygems'
+require 'bacon'
+Bacon.summary_on_exit
+
+F = ::File
+D = ::Dir
+ROOT = F.dirname(__FILE__)+'/..'
+$:.unshift(ROOT+'/lib')
+
+require 'sequel'
+::Sequel::Model.plugin :crushyform
+DB = ::Sequel.sqlite
+
+class Haiku < ::Sequel::Model
+ plugin :schema
+ set_schema do
+ primary_key :id
+ String :title, :crushyform=>{:type=>:custom}
+ text :body
+ Boolean :published, :default => true
+ foreign_key :author_id, :authors
+ end
+ create_table unless table_exists?
+ many_to_one :author
+ one_to_many :reviews
+ def validate
+
+ end
+end
+
+class Author < ::Sequel::Model
+ plugin :schema
+ set_schema do
+ primary_key :id
+ String :name
+ end
+ create_table unless table_exists?
+ one_to_many :haikus
+end
+
+DB.create_table :reviews do
+ primary_key :id
+ String :title
+ text :body
+ Integer :rate
+ foreign_key :haiku_id, :haikus
+end
+class Review < ::Sequel::Model
+ many_to_one :haiku
+end
+
+describe 'Crushyform when schema plugin is not used' do
+
+ should 'have a correct default crushyform_schema' do
+ Review.default_crushyform_schema.should=={
+ :id => {:type=>:integer},
+ :title => {:type=>:string},
+ :body => {:type=>:text},
+ :rate => {:type=>:integer},
+ :haiku_id => {:type=>:parent}
+ }
+ end
+
+ should 'build the default crushyform_schema on the first query' do
+ Review.respond_to?(:schema).should==false
+ Review.crushyform_schema.should==Review.default_crushyform_schema
+ end
+
+ should 'have an updatable crushyform_schema' do
+ Review.crushyform_schema[:body][:type] = :custom
+ Review.crushyform_schema.should.not==Review.default_crushyform_schema
+ Review.crushyform_schema[:body][:type] = :text
+ end
+
+end
+
+describe 'Crushyform when schema plugin is used' do
+
+ should 'use schema declaration for building the default crushyform_schema' do
+ Haiku.default_crushyform_schema.should=={
+ :id => {:type=>:integer},
+ :title => {:type=>:custom},
+ :body => {:type=>:text},
+ :published => {:type=>:boolean},
+ :author_id => {:type=>:parent}
+ }
+ end
+
+end
+

0 comments on commit c7623a0

Please sign in to comment.