# File lib/rails_db/table.rb, line 41 def primary_key RailsDb::Database.primary_key(name) end
class RailsDb::Table
Attributes
Public Class Methods
Source
# File lib/rails_db/table.rb, line 52 def self.model_name ActiveModel::Name.new(self, nil, table_name) end
Source
# File lib/rails_db/table.rb, line 17 def initialize(table_name) throw 'Access Denied' unless RailsDb::Database.accessible_tables.include?(table_name) @name = table_name @data = RailsDb::TableData.new(self) self end
Public Instance Methods
Source
# File lib/rails_db/table.rb, line 73 def add_ransack_methods(klass) klass.define_singleton_method(:ransackable_attributes) do |auth_object = nil| column_names.map(&:to_sym) + column_names end klass.define_singleton_method(:ransortable_attributes) do |auth_object = nil| column_names.map(&:to_sym) + column_names end klass.define_singleton_method(:ransackable_associations) do |auth_object = nil| [] end end
Source
# File lib/rails_db/table.rb, line 69 def as_model @model ||= create_model(name) end
Source
# File lib/rails_db/table.rb, line 49 def create_model(table_name, &block) begin klass = Class.new(ActiveRecord::Base) do def self.model_name ActiveModel::Name.new(self, nil, table_name) end self.table_name = table_name self.inheritance_column = nil end klass.count # verify that it works, if not load other, hack rescue klass = ActiveRecord::Base.descendants.detect { |c| c.table_name == table_name } end add_ransack_methods(klass) if ransack_version >= Gem::Version.new('4.0.0') klass.class_eval(&block) if block_given? klass end
Source
# File lib/rails_db/table.rb, line 45 def delete(id) RailsDb::Database.delete(name, primary_key, id) end
Source
# File lib/rails_db/table.rb, line 33 def indexes RailsDb::Database.indexes(name) end
Source
Source
# File lib/rails_db/table.rb, line 87 def ransack_version Gem.loaded_specs['ransack'].version end
Source
# File lib/rails_db/table.rb, line 24 def to_csv CSV.generate do |csv| csv << column_names data.data.rows.each do |row| csv << row end end end
Source
# File lib/rails_db/table.rb, line 37 def truncate RailsDb::Database.truncate(name) end