Derived Activerecord Ruby Class

2019-09-09 23:28发布

I am totally new to ruby. I am trying to make a RESTful service for task tracking application. I researched and found Sinatra better for the job than rails. So I am using Sinatra and ActiveRecord. I am following Up and Running With Sinatra and ActiveRecord. I will be creating the client application in .NET using Restsharp. But this is all about server side.

This is the migration I have created

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

And the Main 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

Currently this code only reads data. I have not started writing of data. Basically the relations are like for a task there will be a developer and reviewer. It will have documents attached to it. It will also have comments. Comments can be on the task or in reply to a comment. As you can see I have declared a Person class and derived Developer and Reviewer from it. Is it the right way? Any other suggestions are welcome. Even suggestions to use other frameworks are accepted.

0条回答
登录 后发表回答