Class: RubyMVC::Models::ActiveRecordTableModel
Overview
This class provides an adapter between the ActiveRecord model classes and the RubyMVC TableModel interface. This class is designed to be able to provide access to either all instances of a particular AR table or only a subset of the rows matching a particular query.
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from Model
#[]=, adapt, #each_label, #is_editable?, #label_for, #labels
#signal, #signals, #valid_signal!, #valid_signal?
#signal_connect, #signal_disconnect, #signal_emit
Constructor Details
Returns a new instance of ActiveRecordTableModel.
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/ruby_mvc/models/ar_table_model.rb', line 38
def initialize(entity_type, rows = nil)
super()
@row_idx = []
@entity_type = entity_type
@rows = rows
@keys = @entity_type.attribute_names.sort
self.each { |r| }
end
|
Instance Attribute Details
#keys ⇒ Object
Returns the value of attribute keys.
36
37
38
|
# File 'lib/ruby_mvc/models/ar_table_model.rb', line 36
def keys
@keys
end
|
Instance Method Details
#[](index) ⇒ Object
95
96
97
|
# File 'lib/ruby_mvc/models/ar_table_model.rb', line 95
def [](index)
@row_idx[index]
end
|
#create_rows(count = 1) ⇒ Object
50
51
52
53
54
|
# File 'lib/ruby_mvc/models/ar_table_model.rb', line 50
def create_rows(count = 1)
rows = []
count.times { rows << @entity_type.new }
rows
end
|
#each(&block) ⇒ Object
99
100
101
102
103
104
105
|
# File 'lib/ruby_mvc/models/ar_table_model.rb', line 99
def each(&block)
@row_idx.clear
_rows.each do |row|
@row_idx << row
block.call(row)
end
end
|
#each_with_index(&block) ⇒ Object
107
108
109
110
111
112
113
|
# File 'lib/ruby_mvc/models/ar_table_model.rb', line 107
def each_with_index(&block)
@row_idx.clear
_rows.each_with_index do |row, i|
@row_idx << row
block.call(row, i)
end
end
|
#insert_row(index, row) ⇒ Object
56
57
58
59
|
# File 'lib/ruby_mvc/models/ar_table_model.rb', line 56
def insert_row(index, row)
@row_idx.insert(index, row)
super(index, row)
end
|
#insert_rows(index, rows) ⇒ Object
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/ruby_mvc/models/ar_table_model.rb', line 61
def insert_rows(index, rows)
if index == -1
@row_idx.concat(rows)
else
rows.each_with_index do |row, i|
@row_idx.insert(index + i, row)
end
end
super(index, rows)
end
|
#remove_row(index) ⇒ Object
72
73
74
75
76
77
78
|
# File 'lib/ruby_mvc/models/ar_table_model.rb', line 72
def remove_row(index)
if row = @row_idx.delete_at(index)
@entity_type.delete(row[@entity_type.primary_key])
signal_emit("rows-removed", self, index, [ row ])
end
row
end
|
#remove_rows(index, count) ⇒ Object
80
81
82
83
84
85
86
87
88
|
# File 'lib/ruby_mvc/models/ar_table_model.rb', line 80
def remove_rows(index, count)
rows = []
count.times do
rows << @row_idx.delete_at(index)
@entity_type.delete(rows.last[@entity_type.primary_key])
end
signal_emit("rows-removed", self, index, rows)
rows
end
|
#size ⇒ Object
119
120
121
122
|
# File 'lib/ruby_mvc/models/ar_table_model.rb', line 119
def size
puts "idx.size: #{@row_idx.size}"
[_rows.count, @row_idx.size].max
end
|
#update_row(index, row) ⇒ Object
90
91
92
93
|
# File 'lib/ruby_mvc/models/ar_table_model.rb', line 90
def update_row(index, row)
@row_idx[index] = row
super
end
|
#value_for(row, key) ⇒ Object
115
116
117
|
# File 'lib/ruby_mvc/models/ar_table_model.rb', line 115
def value_for(row, key)
@row_idx[row][key]
end
|