派生的Activerecord Ruby类

时间:2015-02-01 09:21:32

标签: ruby-on-rails ruby activerecord sinatra sinatra-activerecord

我对红宝石全新。我正在尝试为任务跟踪应用程序创建RESTful服务。我研究并发现Sinatra比铁路更好。所以我使用的是Sinatra和ActiveRecord。我关注Up and Running With Sinatra and ActiveRecord。我将使用Restsharp在.NET中创建客户端应用程序。但这完全取决于服务器端。

这是我创建的迁移

class CreateTasksPeopleDocumentsAndComments < ActiveRecord::Migration
  def self.up
    create_table :tasks do |t|
      t.string :name
      t.string :task_status
      t.string :task_type
      t.text :description
      t.text :analysis
      t.text :investigation
      t.integer :developer_id
      t.integer :reviewer_id
      t.date :open_date
      t.date :analysis_date
      t.date :promotion_date
      t.date :review_date
      t.date :correction_date
      t.date :collection_date
      t.date :closed_date
      t.date :modified_date
      t.date :target_date
    end
    create_table :people do |t|
      t.string :name
      t.string :trigram
      t.string :state
      t.string :level
    end
    create_table :documents do |t|
      t.string :name
      t.binary :data
      t.string :path
      t.integer :task_id
      t.integer :developer_id
    end
    create_table :comments do |t|
      t.text :comment
      t.datetime :comment_timestamp
      t.integer :person_id
      t.integer :task_id
      t.integer :comment_id
    end
  end

  def self.down
    drop_tables :tasks
    drop_tables :people
    drop_tables :documents
    drop_tables :comments
  end
end

主App.rb

class Person < ActiveRecord::Base
end

class Developer < Person
  has_many :tasks 
end

class Reviewer < Person
  has_many :tasks
end

class Task < ActiveRecord::Base
  belongs_to :developer
  belongs_to :reviewer
  has_many :documents
end

class Document < ActiveRecord::Base
  belongs_to :task
  belongs_to :developer
end

class Comment < ActiveRecord::Base
  belongs_to :task
  has_many :comments
end

get '/' do
  "Hola World!"
end

get '/tasks' do
  Task.all.to_json
end

get '/people' do
  Person.all.to_json
end

get '/person/:id' do
  Person.where(["id = ?", params[:id]]).to_json
end

get '/task/:id' do
  Task.where(["id = ?", params[:id]]).to_json
end

get '/document/:id' do
  Document.where(["id = ?", params[:id]]).to_json
end

get '/task/:id/documents' do
#  Task.where(["id = ?", params[:id]]).document.all.to_json
#  Document.where(["task_id = ?", params[:id]]).all.to_json
  Task.find(params[:id]).documents.all.to_json
end


get '/make' do
  person1 = Person.create(  
                          :name => "AEonAX",
                          :trigram =>"anx",
                          :state => "Active",
                          :level => "Master"
                         )
  person2 = Person.create(
                          :name => "XEonAX",
                          :trigram =>"xnx",
                          :state => "Inactive",
                          :level => "User"
                         )
  person3 = Person.create(  
                          :name => "ZEonAX",
                          :trigram =>"znx",
                          :state => "Active",
                          :level => "User"
                         )
  person4 = Person.create(
                          :name => "LEonAX",
                          :trigram =>"lnx",
                          :state => "Inactive",
                          :level => "Master"
                         )
  task1 = Task.create(  
                      :name => "IR-000001V0R2000",
                      :description => "The Very First Incident Report",
                      :task_status => "Opened",
                      :task_type => "Internal",
                      :developer_id => person2.id,
                      :reviewer_id => person1.id
                     )
  task2 = Task.create(
                      :name => "IR-000002V0R2000",
                      :description => "Second Incident Report",
                      :task_status => "Tech. Anal.",
                      :task_type => "External",
                      :developer_id => person2.id,
                      :reviewer_id => person1.id                      
                     )
  task3 = Task.create(
                      :name => "IR-000003V0R2000",
                      :description => "Another Incident Report",
                      :task_status => "Under Corr.",
                      :task_type => "External",
                      :developer_id => person3.id,
                      :reviewer_id => person1.id                      
                     ) 
  document1 = Document.create(
                              :name => "FirstDoku",
                              :path => "\\XEON-NB\Test\FiddlerRoot.cer",
                              :task_id => task1.id,
                              :developer_id => task1.developer.id,
                              :data => Task.all.to_json #SomeBinaryData
                             )
end

目前此代码仅读取数据。我还没有开始写数据。 基本上,关系就像一个任务,将有一个开发人员和审查人员。它将附有文件。它也会有评论。 评论可以在任务上或回复评论。 正如您所看到的,我已经声明了Person类,并从中派生了DeveloperReviewer。这是正确的方法吗?欢迎任何其他建议。甚至可以接受使用其他框架的建议。

0 个答案:

没有答案