|
| 1 | +require 'active_record' |
| 2 | +require 'json/api/serializer' |
| 3 | + |
| 4 | +ActiveRecord::Base.establish_connection( |
| 5 | + :adapter => 'sqlite3', |
| 6 | + :database => ':memory:' |
| 7 | +) |
| 8 | + |
| 9 | +ActiveRecord::Schema.define do |
| 10 | + create_table :ar_people, force: true do |t| |
| 11 | + t.string :name |
| 12 | + t.string :email |
| 13 | + t.datetime :date_joined |
| 14 | + t.timestamps |
| 15 | + end |
| 16 | + |
| 17 | + create_table :ar_posts, force: true do |t| |
| 18 | + t.string :title |
| 19 | + t.text :body |
| 20 | + #t.integer :author_id |
| 21 | + t.belongs_to :ar_section, index: true |
| 22 | + t.timestamps |
| 23 | + end |
| 24 | + |
| 25 | + create_table :ar_comments, force: true do |t| |
| 26 | + t.text :body |
| 27 | + t.belongs_to :ar_post, index: true |
| 28 | + #t.integer :author_id |
| 29 | + t.timestamps |
| 30 | + end |
| 31 | + |
| 32 | + create_table :ar_tags, force: true do |t| |
| 33 | + t.string :name |
| 34 | + end |
| 35 | + |
| 36 | + create_table :ar_sections, force: true do |t| |
| 37 | + t.string :name |
| 38 | + end |
| 39 | + |
| 40 | + create_table :ar_posts_tags, force: true do |t| |
| 41 | + t.references :ar_post, :ar_tag, index: true |
| 42 | + end |
| 43 | + |
| 44 | + create_table :ar_comments_tags, force: true do |t| |
| 45 | + t.references :ar_comment, :ar_tag, index: true |
| 46 | + end |
| 47 | +end |
| 48 | + |
| 49 | +#class ARPerson < ActiveRecord::Base |
| 50 | +# has_many :ar_posts, class_name: 'ARPost' |
| 51 | +# has_many :ar_comments, class_name: 'ARComment' |
| 52 | +#end |
| 53 | + |
| 54 | +class ARPost < ActiveRecord::Base |
| 55 | + #belongs_to :author, class_name: 'ARPerson', foreign_key: 'author_id' |
| 56 | + has_many :ar_comments, class_name: 'ARComment' |
| 57 | + has_and_belongs_to_many :ar_tags, class_name: 'ARTag', join_table: :ar_posts_tags |
| 58 | + belongs_to :ar_section, class_name: 'ARSection' |
| 59 | +end |
| 60 | + |
| 61 | +class ARComment < ActiveRecord::Base |
| 62 | + #belongs_to :author, class_name: 'ARPerson', foreign_key: 'author_id' |
| 63 | + belongs_to :ar_post, class_name: 'ARPost' |
| 64 | + has_and_belongs_to_many :ar_tags, class_name: 'ARTag', join_table: :ar_comments_tags |
| 65 | +end |
| 66 | + |
| 67 | +class ARTag < ActiveRecord::Base |
| 68 | +end |
| 69 | + |
| 70 | +class ARSection < ActiveRecord::Base |
| 71 | +end |
| 72 | + |
| 73 | +#class ARPersonSerializer < ActiveModel::Serializer |
| 74 | +# attributes :name, :email, :date_joined |
| 75 | +#end |
| 76 | + |
| 77 | +class ARPostSerializer < JSON::API::Serializer |
| 78 | + #attributes :id, :title, :body |
| 79 | + # |
| 80 | + #has_many :ar_comments, :ar_tags |
| 81 | + #has_one :ar_section |
| 82 | + #has_one :author, class_name: 'ARPeople' |
| 83 | +end |
| 84 | + |
| 85 | +class ARCommentSerializer < JSON::API::Serializer |
| 86 | + #attributes :id, :body |
| 87 | + #has_one :ar_post |
| 88 | + #has_many :ar_tags |
| 89 | +end |
| 90 | + |
| 91 | +class ARTagSerializer < JSON::API::Serializer |
| 92 | + #attributes :id, :name |
| 93 | +end |
| 94 | + |
| 95 | +class ARSectionSerializer < JSON::API::Serializer |
| 96 | + #attributes 'name' |
| 97 | +end |
| 98 | + |
| 99 | +#a = ARPerson.create(name: 'Joe Author', |
| 100 | + |
| 101 | +# date_joined: DateTime.parse('2013-08-07 20:25:00 UTC +00:00')) |
| 102 | + |
| 103 | +ARPost.create(title: 'New post', |
| 104 | + body: 'A body!!!', |
| 105 | + #author_id: a.id, |
| 106 | + ar_section: ARSection.create(name: 'ruby')).tap do |post| |
| 107 | + |
| 108 | + short_tag = post.ar_tags.create(name: 'short') |
| 109 | + whiny_tag = post.ar_tags.create(name: 'whiny') |
| 110 | + happy_tag = ARTag.create(name: 'happy') |
| 111 | + |
| 112 | + post.ar_comments.create(body: 'what a dumb post').tap do |comment| |
| 113 | + comment.ar_tags.concat happy_tag, whiny_tag |
| 114 | + end |
| 115 | + |
| 116 | + post.ar_comments.create(body: 'i liked it').tap do |comment| |
| 117 | + comment.ar_tags.concat happy_tag, short_tag |
| 118 | + end |
| 119 | +end |
0 commit comments