create samples table model and migration

This commit is contained in:
Mojca Lorber 2016-12-05 11:08:33 +01:00
parent 33adb58950
commit 01e75fbbcc
5 changed files with 101 additions and 0 deletions

View file

@ -15,6 +15,7 @@ class Organization < ActiveRecord::Base
has_many :user_organizations, inverse_of: :organization, dependent: :destroy
has_many :users, through: :user_organizations
has_many :samples, inverse_of: :organization
has_many :samples_tables, inverse_of: :organization, dependent: :destroy
has_many :sample_groups, inverse_of: :organization
has_many :sample_types, inverse_of: :organization
has_many :logs, inverse_of: :organization

View file

@ -0,0 +1,9 @@
class SamplesTable < ActiveRecord::Base
validates :user, :organization, presence: true
belongs_to :user, inverse_of: :samples_tables
belongs_to :organization, inverse_of: :samples_tables
scope :find_status,
->(org, user) { where(user: user, organization: org).pluck(:status) }
end

View file

@ -46,6 +46,7 @@ class User < ActiveRecord::Base
has_many :activities, inverse_of: :user
has_many :results, inverse_of: :user
has_many :samples, inverse_of: :user
has_many :samples_tables, inverse_of: :user, dependent: :destroy
has_many :steps, inverse_of: :user
has_many :custom_fields, inverse_of: :user
has_many :reports, inverse_of: :user

View file

@ -0,0 +1,79 @@
class CreateSamplesTables < ActiveRecord::Migration
@@def_state = { 'time' => 0,
'start' => 0,
'length' => 10,
'order' => [[2, 'desc']],
'search' => { 'search' => '',
'smart' => true,
'regex' => false,
'caseInsensitive' => true },
'columns' => [{ 'visible' => true,
'search' => { 'search' => '',
'smart' => true,
'regex' => false,
'caseInsensitive' => true } },
{ 'visible' => true,
'search' => { 'search' => '',
'smart' => true,
'regex' => false,
'caseInsensitive' => true } },
{ 'visible' => true,
'search' => { 'search' => '',
'smart' => true,
'regex' => false,
'caseInsensitive' => true } },
{ 'visible' => true,
'search' => { 'search' => '',
'smart' => true,
'regex' => false,
'caseInsensitive' => true } },
{ 'visible' => true,
'search' => { 'search' => '',
'smart' => true,
'regex' => false,
'caseInsensitive' => true } },
{ 'visible' => true,
'search' => { 'search' => '',
'smart' => true,
'regex' => false,
'caseInsensitive' => true } },
{ 'visible' => true,
'search' => { 'search' => '',
'smart' => true,
'regex' => false,
'caseInsensitive' => true } }],
'ColReorder' => [0, 1, 2, 3, 4, 5, 6] }
def change
create_table :samples_tables do |t|
t.jsonb :status, null: false, default: @@def_state
# Foreign keys
t.references :user, null: false
t.references :organization, null: false
t.timestamps null: false
end
add_index :samples_tables, :user_id
add_index :samples_tables, :organization_id
User.find_each do |user|
next unless user.organizations
user.organizations.find_each do |org|
org_status = @@def_state.deep_dup
next unless org.custom_fields
org.custom_fields.each_with_index do |_, index|
org_status['columns'] << { 'visible' => true,
'search' => { 'search' => '',
'smart' => true,
'regex' => false,
'caseInsensitive' => true } }
org_status['ColReorder'] << (7 + index)
end
SamplesTable.create(user: user, organization: org, status: org_status)
end
end
end
end

View file

@ -497,6 +497,17 @@ ActiveRecord::Schema.define(version: 20161125153600) do
add_index "samples", ["sample_type_id"], name: "index_samples_on_sample_type_id", using: :btree
add_index "samples", ["user_id"], name: "index_samples_on_user_id", using: :btree
create_table "samples_tables", force: :cascade do |t|
t.jsonb "status", default: {"time"=>0, "order"=>[[2, "desc"]], "start"=>0, "length"=>10, "search"=>{"regex"=>false, "smart"=>true, "search"=>"", "caseInsensitive"=>true}, "columns"=>[{"search"=>{"regex"=>false, "smart"=>true, "search"=>"", "caseInsensitive"=>true}, "visible"=>true}, {"search"=>{"regex"=>false, "smart"=>true, "search"=>"", "caseInsensitive"=>true}, "visible"=>true}, {"search"=>{"regex"=>false, "smart"=>true, "search"=>"", "caseInsensitive"=>true}, "visible"=>true}, {"search"=>{"regex"=>false, "smart"=>true, "search"=>"", "caseInsensitive"=>true}, "visible"=>true}, {"search"=>{"regex"=>false, "smart"=>true, "search"=>"", "caseInsensitive"=>true}, "visible"=>true}, {"search"=>{"regex"=>false, "smart"=>true, "search"=>"", "caseInsensitive"=>true}, "visible"=>true}, {"search"=>{"regex"=>false, "smart"=>true, "search"=>"", "caseInsensitive"=>true}, "visible"=>true}], "ColReorder"=>[0, 1, 2, 3, 4, 5, 6]}, null: false
t.integer "user_id", null: false
t.integer "organization_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "samples_tables", ["organization_id"], name: "index_samples_tables_on_organization_id", using: :btree
add_index "samples_tables", ["user_id"], name: "index_samples_tables_on_user_id", using: :btree
create_table "step_assets", force: :cascade do |t|
t.integer "step_id", null: false
t.integer "asset_id", null: false