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, #link_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
49
|
# File 'lib/ruby_mvc/models/ar_table_model.rb', line 38
def initialize(entity_type, options = {})
super()
@row_idx = []
@entity_type = entity_type
@rows = options[:rows]
@filter = options[:filter]
@keys = @entity_type.attribute_names.sort
self.each { |r| }
end
|
Instance Attribute Details
#entity_type ⇒ Object
Returns the value of attribute entity_type.
36
37
38
|
# File 'lib/ruby_mvc/models/ar_table_model.rb', line 36
def entity_type
@entity_type
end
|
#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
96
97
98
|
# File 'lib/ruby_mvc/models/ar_table_model.rb', line 96
def [](index)
@row_idx[index]
end
|
#create_rows(count = 1) ⇒ Object
51
52
53
54
55
|
# File 'lib/ruby_mvc/models/ar_table_model.rb', line 51
def create_rows(count = 1)
rows = []
count.times { rows << @entity_type.new(@filter) }
rows
end
|
#each(&block) ⇒ Object
100
101
102
103
104
105
106
|
# File 'lib/ruby_mvc/models/ar_table_model.rb', line 100
def each(&block)
@row_idx.clear
_rows.each do |row|
@row_idx << row
block.call(row)
end
end
|
#each_with_index(&block) ⇒ Object
108
109
110
111
112
113
114
|
# File 'lib/ruby_mvc/models/ar_table_model.rb', line 108
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
57
58
59
60
|
# File 'lib/ruby_mvc/models/ar_table_model.rb', line 57
def insert_row(index, row)
@row_idx.insert(index, row)
super(index, row)
end
|
#insert_rows(index, rows) ⇒ Object
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/ruby_mvc/models/ar_table_model.rb', line 62
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
73
74
75
76
77
78
79
|
# File 'lib/ruby_mvc/models/ar_table_model.rb', line 73
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
81
82
83
84
85
86
87
88
89
|
# File 'lib/ruby_mvc/models/ar_table_model.rb', line 81
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
120
121
122
123
|
# File 'lib/ruby_mvc/models/ar_table_model.rb', line 120
def size
[_rows.count, @row_idx.size].max
end
|
#update_row(index, row) ⇒ Object
91
92
93
94
|
# File 'lib/ruby_mvc/models/ar_table_model.rb', line 91
def update_row(index, row)
@row_idx[index] = row
super
end
|
#value_for(row, key) ⇒ Object
116
117
118
|
# File 'lib/ruby_mvc/models/ar_table_model.rb', line 116
def value_for(row, key)
@row_idx[row][key]
end
|