Class: Crudboy::Model

Inherits:
Object show all
Defined in:
lib/crudboy/model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(active_record_model, table_name, table_comment) ⇒ Model

Returns a new instance of Model.



5
6
7
8
9
10
11
# File 'lib/crudboy/model.rb', line 5

def initialize(active_record_model, table_name, table_comment)
  @active_record_model = active_record_model
  @name = @active_record_model.name
  @table_name = table_name
  @table_comment = table_comment
  @columns = active_record_model.columns.map { |c| Column.new(c, c.name == active_record_model.primary_key) }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, **options, &block) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/crudboy/model.rb', line 47

def method_missing(method, *args, **options, &block)
  if active_record_model.respond_to?(method)
    active_record_model.send(method, *args, **options, &block)
  else
    super
  end
end

Instance Attribute Details

#active_record_modelObject

Returns the value of attribute active_record_model.



3
4
5
# File 'lib/crudboy/model.rb', line 3

def active_record_model
  @active_record_model
end

#columns(**options) ⇒ Object

Returns the value of attribute columns.



3
4
5
# File 'lib/crudboy/model.rb', line 3

def columns
  @columns
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/crudboy/model.rb', line 3

def name
  @name
end

#table_commentObject

Returns the value of attribute table_comment.



3
4
5
# File 'lib/crudboy/model.rb', line 3

def table_comment
  @table_comment
end

#table_nameObject

Returns the value of attribute table_name.



3
4
5
# File 'lib/crudboy/model.rb', line 3

def table_name
  @table_name
end

Instance Method Details

#primary_columnObject



13
14
15
# File 'lib/crudboy/model.rb', line 13

def primary_column
  columns.find { |c| c.name == active_record_model.primary_key }
end

#regular_columnsObject



17
18
19
# File 'lib/crudboy/model.rb', line 17

def regular_columns
  columns.reject { |c| c.name == active_record_model.primary_key }
end