Module: Datasource::Adapters::ActiveRecord
- Defined in:
- lib/datasource/adapters/active_record.rb
Constant Summary collapse
- ID_KEY =
"id"
Instance Method Summary collapse
- #ensure_table_join!(scope, name, att) ⇒ Object
- #get_rows(scope) ⇒ Object
- #get_select_values(scope) ⇒ Object
- #included_datasource_rows(att, datasource_data, rows) ⇒ Object
- #to_query(scope) ⇒ Object
Instance Method Details
#ensure_table_join!(scope, name, att) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/datasource/adapters/active_record.rb', line 112 def ensure_table_join!(scope, name, att) join_value = scope.joins_values.find do |value| if value.is_a?(Symbol) value.to_s == att[:name] elsif value.is_a?(String) if value =~ /join (\w+)/i $1 == att[:name] end end end raise "Given scope does not join on #{name}, but it is required by #{att[:name]}" unless join_value end |
#get_rows(scope) ⇒ Object
53 54 55 |
# File 'lib/datasource/adapters/active_record.rb', line 53 def get_rows(scope) scope.pluck_hash(*get_select_values(scope)) end |
#get_select_values(scope) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/datasource/adapters/active_record.rb', line 81 def get_select_values(scope) select_values = Set.new select_values.add("#{scope.klass.table_name}.#{self.class.adapter::ID_KEY}") self.class._attributes.each do |att| if attribute_exposed?(att[:name]) if att[:klass] == nil select_values.add("#{scope.klass.table_name}.#{att[:name]}") elsif att[:klass].ancestors.include?(Attributes::ComputedAttribute) att[:klass]._depends.keys.map(&:to_s).each do |name| next if name == scope.klass.table_name ensure_table_join!(scope, name, att) end att[:klass]._depends.each_pair do |table, names| Array(names).each do |name| select_values.add("#{table}.#{name}") end # TODO: handle depends on virtual attribute end elsif att[:klass].ancestors.include?(Attributes::QueryAttribute) select_values.add("(#{att[:klass].new.select_value}) as #{att[:name]}") att[:klass]._depends.each do |name| next if name == scope.klass.table_name ensure_table_join!(scope, name, att) end end end end select_values.to_a end |
#included_datasource_rows(att, datasource_data, rows) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/datasource/adapters/active_record.rb', line 57 def included_datasource_rows(att, datasource_data, rows) ds_select = datasource_data[:select] unless ds_select.include?(att[:foreign_key]) ds_select += [att[:foreign_key]] end ds_scope = datasource_data[:scope] column = "#{ds_scope.klass.table_name}.#{att[:foreign_key]}" ds_scope = ds_scope.where("#{column} IN (?)", rows.map { |row| row[att[:id_key]] }) grouped_results = att[:klass].new(ds_scope) .select(ds_select) .results.group_by do |row| row[att[:foreign_key]] end unless datasource_data[:select].include?(att[:foreign_key]) grouped_results.each_pair do |k, rows| rows.each do |row| row.delete(att[:foreign_key]) end end end grouped_results end |
#to_query(scope) ⇒ Object
47 48 49 50 51 |
# File 'lib/datasource/adapters/active_record.rb', line 47 def to_query(scope) ActiveRecord::Base.uncached do scope.select(*get_select_values(scope)).to_sql end end |