Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Also handle NOT NULL ... DEFAULT constraints on the DB schema itself.

  • Loading branch information...
commit 931aa95cd60fff0d895634d524b9ff6c1dd9d542 1 parent 703eb7b
@mike-burns authored
View
17 README.md
@@ -106,6 +106,23 @@ Is That All?
This gem uses the `wrapped` gem underneath, which can do a whole lot more than
just `unwrap_or`. Check it out for the details.
+Installation
+------------
+
+Add this to your `Gemfile` the run the `bundle` command:
+
+ gem 'wrapped-rails'
+
+Details
+-------
+
+Adding this gem to your Rails app will affect every model. A field is wrapped
+if two conditions are met:
+
+* there is no `validates_presence_of` on that field, and
+* the database schema does not have a NOT NULL constraint paired with a default
+ value.
+
Copyright
---------
Copyright 2011 Mike Burns
View
66 features/not_null_constraint_validation.feature
@@ -0,0 +1,66 @@
+Feature: NOT NULL ... DEFAULT toggles the wrapped value
+
+ Scenario: An app with a model with some fields that require presence of
+ When I successfully run `bundle exec rails new testapp`
+ And I cd to "testapp"
+ And I add the "capybara" gem
+ And I add the "database_cleaner" gem
+ And I add the "diesel" gem
+ And I add the "wrapped-rails" gem from this project
+ And I run `bundle install --local`
+
+ When I write to "app/controllers/users_controller.rb" with:
+ """
+ class UsersController < ApplicationController
+ def index
+ @users = User.all
+ end
+ end
+ """
+ When I write to "db/migrate/1_create_users.rb" with:
+ """
+ class CreateUsers < ActiveRecord::Migration
+ def self.up
+ create_table :users do |t|
+ t.string :first_name, :last_name, :null => false, :default => ''
+ t.string :middle_name
+ end
+ end
+ end
+ """
+ When I write to "app/models/user.rb" with:
+ """
+ class User < ActiveRecord::Base
+ end
+ """
+ When I write to "app/views/users/index.html.erb" with:
+ """
+ <% @users.each do |user| %>
+ <%= user.first_name %> <%= user.middle_name.unwrap_or('') {|mn| "#{mn.first}." } %> <%= user.last_name %>
+ <% end %>
+ """
+ When I write to "test/integration/the_test.rb" with:
+ """
+ require 'test_helper'
+ class TheTest < ActionController::IntegrationTest
+ def test_the_app
+ User.create!(:first_name => 'Roy', :middle_name => 'Grace', :last_name => 'Biv')
+ User.create!(:first_name => 'The', :last_name => 'Pope')
+
+ get users_path
+
+ assert_match /Roy G\. Biv/m, @response.body
+ assert_match /The Pope/m, @response.body
+ end
+ end
+ """
+ When I write to "config/routes.rb" with:
+ """
+ Testapp::Application.routes.draw do
+ resources :users
+ end
+ """
+
+ When I successfully run `bundle exec rake db:migrate --trace`
+ Then I successfully run `bundle exec rake --trace`
+
View
23 lib/wrapped-rails/wrap_nilables.rb
@@ -11,13 +11,6 @@ def read_attribute(attr_name)
private
- def nilable?(attr_name)
- attr_name != 'id' && \
- self.class.validators_on(attr_name).none? do |v|
- v.is_a?(ActiveModel::Validations::PresenceValidator)
- end
- end
-
def wrap_attributes!
@attributes.each do |attr_name, value|
if nilable?(attr_name)
@@ -27,5 +20,21 @@ def wrap_attributes!
end
end
end
+
+ def nilable?(attr_name)
+ attr_name != 'id' &&
+ !presence_validated?(attr_name) &&
+ !null_constraint?(attr_name)
+ end
+
+ def presence_validated?(attr_name)
+ self.class.validators_on(attr_name).any? do |v|
+ v.is_a?(ActiveModel::Validations::PresenceValidator)
+ end
+ end
+
+ def null_constraint?(attr_name)
+ !self.class.columns_hash[attr_name].default.nil?
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.