Guest User

Untitled

a guest
Feb 20th, 2018
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. ##Migration - 008_create_actors.rb
  2. class CreateActors < ActiveRecord::Migration
  3.  
  4. def self.up
  5. create_table :actors do |table|
  6. table.column :name, :string, :null => false, :limit => 255
  7. table.column :actor_type_id, :integer, :null => false
  8. table.column :project_version_id, :integer, :null => false
  9. table.column :position, :integer, :null => false
  10.  
  11. table.column :created_at, :datetime
  12. table.column :updated_at, :datetime
  13. table.column :lock_version, :integer, :null => false, :default => 0
  14. end
  15.  
  16. add_index :actors, :project_version_id
  17. add_index :actors, :lock_version
  18. add_index :actors, [:name, :project_version_id], :unique => true, :name => 'actors_nameproject_version_id_index'
  19. end
  20.  
  21. def self.down
  22. drop_table :actors
  23. end
  24. end
  25.  
  26. ##Model - actor.rb
  27. class Actor < ActiveRecord::Base
  28. validates_presence_of :name, :project_version_id, :position
  29. validates_length_of :name, :in => 1..255
  30. validates_uniqueness_of :name, :scope => :project_version_id
  31. validates_numericality_of :actor_type_id, :project_version_id, :position, :only_integer => true
  32.  
  33. belongs_to :project_version
  34. belongs_to :actor_type
  35. acts_as_list :scope => :project_version
  36. end
  37.  
  38. ##MySQL info
  39. mysql> describe actors;
  40. +--------------------+--------------+------+-----+---------+----------------+
  41. | Field | Type | Null | Key | Default | Extra |
  42. +--------------------+--------------+------+-----+---------+----------------+
  43. | id | int(11) | NO | PRI | NULL | auto_increment |
  44. | name | varchar(255) | NO | MUL | NULL | |
  45. | actor_type_id | int(11) | NO | | NULL | |
  46. | project_version_id | int(11) | NO | MUL | NULL | |
  47. | position | int(11) | NO | | NULL | |
  48. | created_at | datetime | YES | | NULL | |
  49. | updated_at | datetime | YES | | NULL | |
  50. | lock_version | int(11) | NO | MUL | 0 | |
  51. +--------------------+--------------+------+-----+---------+----------------+
  52. 8 rows in set (0.01 sec)
  53.  
  54. ##Test - actor_test.rb
  55. def test_creation
  56. actor = Actor.new()
  57. assert !actor.valid? #passes
  58. assert actor.errors.invalid?(:name) #passes
  59. assert actor.errors.invalid?(:position) #fails
  60. assert actor.errors.invalid?(:project_version_id) #fails
  61. assert actor.errors.invalid?(:actor_type_id) #fails
  62. end
Add Comment
Please, Sign In to add comment