How to use primary_key method of ClassMethods Package

Best Active_mocker_ruby code snippet using ClassMethods.primary_key

table.rb

Source:table.rb Github

copy

Full Screen

...17 # longer works.18 #19 # A workaround is to create a dummy table for this base class20 self.table_name = "bj_tables"21 self.primary_key = "#{ table_name }_id"22 module ClassMethods23 attribute("list"){ Array.new }24 attribute("migration"){}25 def migration_code classname = "BjMigration"26 <<-code27 class #{ classname } < ActiveRecord::Migration28 def self.up29 Bj::Table.each{|table| table.up}30 end31 def self.down32 Bj::Table.reverse_each{|table| table.down}33 end34 end35 code36 end37 def up38 migration_class.up39 end40 def down41 migration_class.down42 end43 def migration_class44 table = self45 @migration_class ||=46 Class.new(ActiveRecord::Migration) do47 sc =48 class << self49 self50 end51 sc.module_eval{ attribute :table => table }52 sc.module_eval &table.migration53 end54 end55 def content_column_names56 @content_column_names = content_columns.map{|column| column.name}57 end58 def create_hash_for options59 options.to_options!60 hash = {}61 content_column_names.each do |key|62 key = key.to_s.to_sym63 hash[key] = options[key]64 end65 hash66 end67 def each *a, &b68 list.each *a, &b69 end70 def reverse_each *a, &b71 list.reverse.each *a, &b72 end73 end74 send :extend, ClassMethods75 module InstanceMethods76 def to_hash77 oh = OrderedHash.new78 self.class.content_column_names.each{|c| oh[c] = self[c]}79 oh80 end81 end82 send :include, InstanceMethods83 module RecursivelyInherited84 def inherited other85 super86 ensure87 (Table.list << other).uniq!88 basename = other.name.split(%r/::/).last.underscore89 Table.singleton_class{ attribute basename => other }90 other.send :extend, RecursivelyInherited91 end92 end93 send :extend, RecursivelyInherited94#95# table classes96#97 class Job < Table98 self.table_name = "bj_job"99 self.primary_key = "#{ table_name }_id"100 migration { 101 define_method :up do102 create_table table.table_name, :primary_key => table.primary_key, :force => true do |t|103 t.column "command" , :text104 t.column "state" , :text105 t.column "priority" , :integer106 t.column "tag" , :text107 t.column "is_restartable" , :integer108 t.column "submitter" , :text109 t.column "runner" , :text110 t.column "pid" , :integer111 t.column "submitted_at" , :datetime112 t.column "started_at" , :datetime113 t.column "finished_at" , :datetime114 t.column "env" , :text115 t.column "stdin" , :text116 t.column "stdout" , :text117 t.column "stderr" , :text118 t.column "exit_status" , :integer119 end120 end121 define_method :down do122 drop_table table.table_name 123 end124 }125 module ClassMethods126 def submit jobs, options = {}, &block127 jobs = Joblist.for jobs, options128 returned = []129 transaction do130 jobs.each do |job|131 job = create_hash_for(job.reverse_merge(submit_defaults))132 job = create! job 133 returned << (block ? block.call(job) : job)134 end135 end136 returned137 end138 def submit_defaults139 {140 :state => "pending",141 :priority => 0,142 :tag => "",143 :is_restartable => true,144 :submitter => Bj.hostname,145 :submitted_at => Time.now, 146 }147 end148 end149 send :extend, ClassMethods150 module InstanceMethods151 def title152 "job[#{ id }](#{ command })"153 end154 def finished155 reload156 exit_status157 end158 alias_method "finished?", "finished"159 end160 send :include, InstanceMethods161 end162 class JobArchive < Job163 self.table_name = "bj_job_archive"164 self.primary_key = "#{ table_name }_id"165 migration {166 define_method(:up) do167 create_table table.table_name, :primary_key => table.primary_key, :force => true do |t|168 t.column "command" , :text169 t.column "state" , :text170 t.column "priority" , :integer171 t.column "tag" , :text172 t.column "is_restartable" , :integer173 t.column "submitter" , :text174 t.column "runner" , :text175 t.column "pid" , :integer176 t.column "submitted_at" , :datetime177 t.column "started_at" , :datetime178 t.column "finished_at" , :datetime179 t.column "archived_at" , :datetime180 t.column "env" , :text181 t.column "stdin" , :text182 t.column "stdout" , :text183 t.column "stderr" , :text184 t.column "exit_status" , :integer185 end186 end187 define_method(:down) do188 drop_table table.table_name189 end190 }191 end192 # TODO - initialize with a set of global defaults and fallback to those on perhaps '* * key'193 class Config < Table194 self.table_name = "bj_config"195 self.primary_key = "#{ table_name }_id"196 migration {197 define_method(:up) do198 create_table table.table_name, :primary_key => table.primary_key, :force => true do |t|199 t.column "hostname" , :text200 t.column "key" , :text201 t.column "value" , :text202 t.column "cast" , :text203 end204 begin205 add_index table.table_name, %w[ hostname key ], :unique => true206 rescue Exception207 STDERR.puts "WARNING: your database does not support unique indexes on text fields!?"208 end209 end210 define_method(:down) do211 begin212 remove_index table.table_name, :column => %w[ hostname key ] ...

Full Screen

Full Screen

persitence.rb

Source:persitence.rb Github

copy

Full Screen

...3 module Persistence4 def self.included(base)5 base.extend(ClassMethods)6 base.config[:class_key] = :class7 base.config[:primary_key] = "id"8 end9 module ClassMethods10 def primary_key=(key)11 @repo = nil12 config[:primary_key] = key.to_s13 end14 def primary_key(field_name = nil, type = :string, **opts)15 if field_name16 self.primary_key = field_name17 field(field_name, type, **opts)18 else19 config[:primary_key].to_s20 end21 end22 def model_name=(val)23 @repo = nil24 super25 end26 def storage=(val)27 @repo = nil28 @storage = val29 end30 def storage31 @storage ||= Foxy::Env.current.storage32 end33 def class_key=(val)34 @repo = nil35 @class_key = val36 end37 def class_key38 config[:class_key]39 end40 def repository41 @repo ||= config[:repository_class].new(collection: model_name,42 pk: primary_key,43 storage: storage,44 model: self,45 class_key: class_key)46 end47 def find(primary_key)48 repository.find(primary_key)49 end50 def find_or_create(attrs)51 repository.find_or_create(attrs)52 end53 def create(attrs)54 repository.create(attrs)55 end56 def all57 repository.all58 end59 def destroy_all60 repository.destroy_all61 end62 def from_database(attrs)63 new(attrs, persisted: true)64 end65 end66 def initialize(attrs, persisted: false)67 super(attrs)68 @persisted = persisted69 end70 def repository71 self.class.repository72 end73 def save74 @persisted = true75 repository.save(self)76 self77 end78 def destroy79 repository.destroy(self)80 end81 def primary_key82 send(self.class.primary_key)83 end84 def update(attrs)85 assign_attributes(attrs)86 tap(&:save)87 end88 def persisted!89 @persisted = true90 end91 def new?92 !persisted?93 end94 def persisted?95 !!@persisted96 end...

Full Screen

Full Screen

persistence.rb

Source:persistence.rb Github

copy

Full Screen

...9 object = new(attributes)10 object.save11 object12 end13 def primary_key=(value)14 @primary_key = value.to_sym15 end16 def primary_key17 defined?(@primary_key) ? @primary_key : :id18 end19 end20 def save21 if new_record?22 result = self.class.connection.execute(23 <<~SQL24 INSERT INTO #{self.class.table_name}25 (#{@attributes.keys.join(",")})26 VALUES (#{@attributes.values.map { |v| "'#{v}'" }.join(",")})27 RETURNING *;28 SQL29 )30 @attributes[self.class.primary_key] = result.first[self.class.primary_key]31 @new_record = false32 else33 self.class.connection.execute(34 <<~SQL35 UPDATE #{self.class.table_name}36 SET #{@attributes.map { |k, v| "#{k} = #{v.nil? ? 'NULL' : "'#{v}'"}" }.join(",")}37 WHERE id = #{id};38 SQL39 )40 end41 true42 end43 def new_record?44 @new_record...

Full Screen

Full Screen

primary_key

Using AI Code Generation

copy

Full Screen

1ActiveRecord::Base.establish_connection(2user = User.find(1)3ActiveRecord::Base.establish_connection(4user = User.find(1)5ActiveRecord::Base.establish_connection(6user = User.find(1)7ActiveRecord::Base.establish_connection(8user = User.find(1)9ActiveRecord::Base.establish_connection(10user = User.find(1)11ActiveRecord::Base.establish_connection(12user = User.find(1)

Full Screen

Full Screen

primary_key

Using AI Code Generation

copy

Full Screen

1ActiveRecord::Base.establish_connection(2CreateProducts.migrate(:up)3Product.create(:name => "Product 1", :price => 100)4Product.create(:name => "Product 2", :price => 200)5Product.create(:name => "Product 3", :price => 300)6Product.create(:name => "Product 4", :price => 400)7Product.create(:name => "Product 5", :price => 502)8ActiveRecord::Base.establish_connection(9CreateProducts.migrate(:up)10Product.create(:name => "Product 1", :price => 100)11Product.create(:name => "Product 2", :price => 200)12Product.create(:name => "Product 3", :price => 300)13Product.create(:name => "Product 4", :price => 400)14Product.create(:name => "Product 5", :price => 500)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Active_mocker_ruby automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful