Skip to content
Browse files

-- Changed type_attribute to hold all new tokens

-- USAGE updated
  • Loading branch information...
1 parent 86ebea7 commit 3b1d7dfec1cc3db9e6d645446b58d2409494efff @eladmeidar committed
View
31 generators/model_plus/USAGE
@@ -16,14 +16,29 @@ Description:
You can also decorate a type with suffixes that specify further code
generation in the model:
- title:string+a => Adds to an attr_accessible list
- title:string+p => validates_presence_of :title
- posted:boolean+ap => attr_accessible and
- validates_inclusion_of :posted :in => [true, false]
- rating:float+n => validates_numericality_of :rating
- hits:integer+i => validates_numericality_of :hits, :only_integer => true
- name:string+u => validates_uniqueness_of :name
-
+ Adding validations using the +v token:
+ title:string+vp => validates_uniqueness_of :title
+ username:string+vl => validates_length_of :username
+ username:string+vl5 => validates_length_of :username, :minimum => 5
+ rating:float+vn => validates_numericality_of :rating
+ hits:integer+vi => validates_numericality_of :hits, :only_integer => true
+ name:string+vu => validates_uniqueness_of :name
+ name:string+vuc => validates_uniqueness_of :name, :case_sensitive => false
+ password:string+vc => validates_confirmation_of :password and
+ validates_presence_of :password_confirmation
+
+ accessors too:
+ title:string+aa => attr_accessor :title
+ password:string+ap => attr_protected :password
+ You can also use several tokens at once:
+
+ username:string+vl6+vu => generates validates_length_of :username and validates_uniqueness_of :username
+
+ A side from validations, you can add other options to your attributes:
+
+ hits:integer+vi+cd0 => validates_numericality_of :hits, :only_integer => true and in the migration t.hits, :integer, :default => 0
+ name:string+cn => t.name :string, :null => false
+
This generates a model class in app/models, a unit test in test/unit,
a test fixture in test/fixtures/singular_name.yml, and a migration in
db/migrate.
View
47 generators/model_plus/model_plus_generator.rb
@@ -11,13 +11,58 @@ def referenced_by?
end
def base_type
+ #puts self.type.to_s.split('+')[0]
self.type.to_s.split('+')[0]
+ #extract_options[:base_type]
end
def type_attributes
- self.type.to_s.split('+')[1]
+ @option_attributes = Hash.new("")
+ options = self.type.to_s.split('+')
+ options.shift
+ options.each do |option|
+ case option.downcase.slice(0..1)
+ when 'vl'
+ # check if there is a length specified
+ length = option.slice(2..option.size)
+ @option_attributes['vl'] = length
+ when 'vu'
+ caps_flag = option.last
+ if caps_flag == "c"
+ @option_attributes['vu'] = ", :case_sensitive => false"
+ else
+ @option_attributes['vu'] = ""
+ end
+ when 'cd'
+ default = option.slice(2..option.size)
+ @option_attributes['cd'] = ", :default => #{default}"
+ when 'cn'
+ @option_attributes['cn'] = ", :null => false"
+ else
+ @option_attributes[option] = nil
+ end
+ end
+ # puts @option_attributes.inspect
+ @option_attributes
end
+ def option(option_name)
+
+ end
+
+ def extract_options
+ type_information = self.type_attributes
+
+ extended_parameters = /([(]+[a-zA-z: ,]+[)]+)/.match(type_information)[0] rescue nil
+
+ #unless extended_parameters.blank?
+ # extended_parameters.split(',').each do |option_type|
+ # option_type.strip!
+ # name, value = option_type.split(":")
+ # options[name.to_sym] = value
+ # end
+ #end
+ end
end
end
end
View
2 generators/model_plus/templates/migration.rb
@@ -7,7 +7,7 @@ def self.up
create_table :<%= table_name %> do |t|
<% for attribute in attributes -%>
<% unless attribute.referenced_by? %>
- t.<%= attribute.base_type %> :<%= attribute.name %>
+ t.<%= attribute.base_type %> :<%= attribute.name %><%= attribute.type_attributes['cd'] if attribute.type_attributes.has_key?('cd')%><%= attribute.type_attributes['cn'] if attribute.type_attributes.has_key?('cn')%>
<% end -%>
<% end -%>
<% unless options[:skip_timestamps] %>
View
35 generators/model_plus/templates/model.rb
@@ -5,21 +5,42 @@ class <%= class_name %> < ActiveRecord::Base
<% attributes.select(&:referenced_by?).each do |attribute| -%>
<%= attribute.base_type %> :<%= attribute.name.downcase %>
<% end -%>
-<% attr_accessible = present = bool_present = numerical = integral = unique = [] %>
+<% attr_accessible = attr_protected = confirmation = present = length = bool_present = numerical = integral = unique = [] %>
<% attributes.each do |attribute| -%>
- <% attr_accessible << ":" + attribute.name if attribute.type_attributes =~ /a/ -%>
- <% numerical << ":" + attribute.name if attribute.type_attributes =~ /n/ -%>
- <% integral << ":" + attribute.name if attribute.type_attributes =~ /i/ -%>
- <% unique << ":" + attribute.name if attribute.type_attributes =~ /u/ -%>
- <% present << ":" + attribute.name if attribute.type_attributes =~ /p/ && attribute.base_type != "boolean" -%>
- <% bool_present << ":" + attribute.name if attribute.type_attributes =~ /p/ && attribute.base_type == "boolean" -%>
+ <% attr_accessible << ":" + attribute.name if attribute.type_attributes.has_key?('aa') -%>
+ <% attr_protected << ":" + attribute.name if attribute.type_attributes.has_key?('ap') -%>
+ <% numerical << ":" + attribute.name if attribute.type_attributes.has_key?('vn') -%>
+ <% confirmation << ":" + attribute.name if attribute.type_attributes.has_key?('vc') -%>
+ <% integral << ":" + attribute.name if attribute.type_attributes.has_key?('vi') -%>
+ <% unique << ":" + attribute.name + attribute.type_attributes['vu'] if attribute.type_attributes.has_key?('vu') -%>
+ <% length << ":" + attribute.name + ", :minimum => #{attribute.type_attributes['vl']}" if attribute.type_attributes.has_key?('vl') -%>
+ <% present << ":" + attribute.name if attribute.type_attributes.has_key?('vp') && attribute.base_type != "boolean" -%>
+ <% bool_present << ":" + attribute.name if attribute.type_attributes.has_key?('vp') && attribute.base_type == "boolean" -%>
<% end -%>
<% if !attr_accessible.empty? -%>
attr_accessible <%= attr_accessible.join(", ") %>
<% end -%>
+<% if !attr_protected.empty? -%>
+ attr_protected <%= attr_protected.join(", ") %>
+<% end -%>
+
<% if !present.empty? -%>
validates_presence_of <%= present.join(", ") %>
<% end -%>
+
+<% if !length.empty? -%>
+ <% length.each do |length_validated| %>
+ validates_length_of <%= length_validated %>
+ <% end %>
+<% end -%>
+
+<% if !confirmation.empty? -%>
+ <% confirmation.each do |confirmation_validated| %>
+ validates_confirmation_of <%= confirmation %>
+ validates_presence_of <%= confirmation %>_confirmation
+ <% end %>
+<% end -%>
+
<% if !bool_present.empty? -%>
validates_inclusion_of <%= bool_present.join(", ") %>, :in => [true, false]
<% end -%>
View
1 test/generator_test_helper.rb
@@ -96,6 +96,7 @@ def build_generator(name, params)
# Runs the +create+ command (like the command line does).
def run_generator(name, params)
silence_generator do
+ # puts "gen params: #{params.inspect}"
build_generator(name, params).command(:create).invoke!
end
end
View
117 test/model_plus_test.rb
@@ -55,6 +55,7 @@ def test_model_with_belongs_to_attributes_generates_add_index_statement
assert_generated_index t, :products, :supplier
end
end
+
def test_model_with_has_many_attributes_generates_has_many_associations
run_generator('model_plus', %w(Supplier name:string products:has_many))
@@ -79,6 +80,122 @@ def test_model_with_has_and_belongs_to_many_attributes_generates_habtm_associati
end
end
+
+ def test_model_extended_options_attr_accessor
+ run_generator('model_plus', %w(Product name:string+aa))
+ assert_generated_model_for :product do |body|
+ assert body =~ /^\s+attr_accessible :name/, "#{body.inspect} should contain 'attr_accessible :name'"
+ end
+ end
+
+ def test_model_extended_options_attr_protected
+ run_generator('model_plus', %w(Product name:string+ap))
+ assert_generated_model_for :product do |body|
+ assert body =~ /^\s+attr_protected :name/, "#{body.inspect} should contain 'attr_protected :name'"
+ end
+ end
+
+ def test_model_extended_options_validates_presence_of
+ run_generator('model_plus', %w(Product name:string+vp))
+ assert_generated_model_for :product do |body|
+ assert body =~ /^\s+validates_presence_of :name/, "#{body.inspect} should contain 'validates_presence_of :name'"
+ end
+ end
+
+ def test_model_extended_options_validates_length_of
+ run_generator('model_plus', %w(Product name:string+vl))
+ assert_generated_model_for :product do |body|
+ assert body =~ /^\s+validates_length_of :name/, "#{body.inspect} should contain 'validates_length_of :name'"
+ end
+ end
+
+ def test_model_extended_options_validates_length_of_with_length
+ run_generator('model_plus', %w(Product name:string+vl5))
+ assert_generated_model_for :product do |body|
+ assert body =~ /^\s+validates_length_of :name, :minimum => 5/, "#{body.inspect} should contain 'validates_length_of :name, :minimum => 5'"
+ end
+ end
+
+ def test_model_extended_options_validates_numericality_of
+ run_generator('model_plus', %w(Product age:integer+vn))
+ assert_generated_model_for :product do |body|
+ assert body =~ /^\s+validates_numericality_of :age/, "#{body.inspect} should contain 'validates_numericality_of :age'"
+ end
+ end
+
+ def test_model_extended_options_validates_numericality_of_integer
+ run_generator('model_plus', %w(Product age:integer+vi))
+ assert_generated_model_for :product do |body|
+ assert body =~ /^\s+validates_numericality_of :age, :only_integer => true/, "#{body.inspect} should contain 'validates_numericality_of :age, :only_integer => true'"
+ end
+ end
+
+ def test_model_extended_options_validates_uniqueness_of
+ run_generator('model_plus', %w(Product username:string+vu))
+ assert_generated_model_for :product do |body|
+ assert body =~ /^\s+validates_uniqueness_of :username/, "#{body.inspect} should contain 'validates_uniqueness_of :username'"
+ end
+ end
+
+ def test_model_extended_options_validates_uniqueness_of_no_case_sensitive
+ run_generator('model_plus', %w(Product username:string+vuc))
+ assert_generated_model_for :product do |body|
+ assert body =~ /^\s+validates_uniqueness_of :username, :case_sensitive => false/, "#{body.inspect} should contain 'validates_uniqueness_of :username, :case_sensitive => false'"
+ end
+ end
+
+ def test_model_extended_options_validates_uniqueness_of_no_case_sensitive
+ run_generator('model_plus', %w(Product email:string+vc))
+ assert_generated_model_for :product do |body|
+ assert body =~ /^\s+validates_confirmation_of :email/, "#{body.inspect} should contain 'validates_confirmation_of :email'"
+ assert body =~ /^\s+validates_presence_of :email_confirmation/, "#{body.inspect} should contain 'validates_presence_of :email_confirmation'"
+ end
+ end
+
+ def test_migration_with_default
+ run_generator('model_plus', %w(Product name:string+vl3 age:integer+cd5))
+
+ assert_generated_migration :create_products do |t|
+ assert_generated_column t, 'age, :default => 5', :integer
+ assert_generated_column t, 'name', :string
+ end
+ end
+
+ def test_migration_with_default_empty
+ run_generator('model_plus', %w(Product name:string+vl3 age:integer+cd))
+
+ assert_generated_migration :create_products do |t|
+ assert_generated_column t, 'age', :integer
+ assert_generated_column t, 'name', :string
+ end
+ end
+
+ def test_migration_with_not_null
+ run_generator('model_plus', %w(Product name:string+vl3 age:integer+cn))
+
+ assert_generated_migration :create_products do |t|
+ assert_generated_column t, 'age, :null => false', :integer
+ assert_generated_column t, 'name', :string
+ end
+ end
+
+ def test_migration_with_not_null_and_default
+ run_generator('model_plus', %w(Product name:string+vl3 age:integer+cn+cd5))
+
+ assert_generated_migration :create_products do |t|
+ assert_generated_column t, 'age, :default => 5, :null => false', :integer
+ assert_generated_column t, 'name', :string
+ end
+ end
+
+ def test_migration_with_string_default
+ run_generator('model_plus', %w(Product name:string+cd"elad"))
+
+ assert_generated_migration :create_products do |t|
+ assert_generated_column t, 'name, :default => "elad"', :string
+ end
+ end
+
def test_has_referencing_attributes_do_not_add_column_to_migration
run_generator('model_plus', %w(Supplier name:string products:has_many account:has_one parts:has_and_belongs_to_many))

0 comments on commit 3b1d7df

Please sign in to comment.
Something went wrong with that request. Please try again.