Methods
Public Instance methods
[ show source ]
# File kirbybase_adapter.rb, line 1118
1118: def construct_sql
1119: if @options[:finder_sql]
1120: @custom_finder_sql = lambda{|rec| @options[:finder_sql][rec, @owner, @association_class.find(rec.send(@association_class_primary_key_name))] }
1121: else
1122: # Need to run @join_sql as well - see #find above
1123: @finder_sql = @association_class.build_conditions_from_options(@options)
1124: end
1125:
1126: # this should be run on the join_table
1127: # "LEFT JOIN #{@join_table} ON #{@association_class.table_name}.#{@association_class.primary_key} = #{@join_table}.#{@association_foreign_key}"
1128: @join_sql = lambda{|rec| rec.send(@association_class_primary_key_name) == @owner.id}
1129: end
[ show source ]
# File kirbybase_adapter.rb, line 1094
1094: def delete_records(records)
1095: if sql = @options[:delete_sql]
1096: delete_conditions = if sql.is_a?(Proc)
1097: sql
1098: else
1099: association_selector = @association_class.build_conditions_from_options(:conditions => sql)
1100: lambda do |join_rec, owner, record|
1101: rec.send(@association_foreign_key) == @owner.id &&
1102: record = @associtation_class.find(rec.send(@association_class_primary_key_name)) &&
1103: association_selector[record]
1104: end
1105: end
1106: records.each do |record|
1107: delete_selector = lambda{|join_rec| delete_conditions[join_rec, @owner, record]}
1108: @owner.connection.db.get_table(@join_table.to_sym).delete(&delete_selector)
1109: end
1110: else
1111: ids = records.map { |rec| rec.id }
1112: @owner.connection.db.get_table(@join_table.to_sym).delete do |rec|
1113: rec.send(@association_class_primary_key_name) == @owner.id && ids.include?(rec.send(@association_foreign_key))
1114: end
1115: end
1116: end
[ show source ]
# File kirbybase_adapter.rb, line 1016
1016: def find(*args)
1017: options = ActiveRecord::Base.send(:extract_options_from_args!, args)
1018:
1019: # If using a custom finder_sql, scan the entire collection.
1020: if @options[:finder_sql]
1021: expects_array = args.first.kind_of?(Array)
1022: ids = args.flatten.compact.uniq
1023:
1024: if ids.size == 1
1025: id = ids.first.to_i
1026: record = load_target.detect { |record| id == record.id }
1027: if expects_array
1028: [record].compact
1029: elsif record.nil?
1030: raise RecordNotFound
1031: else
1032: record
1033: end
1034: else
1035: load_target.select { |record| ids.include?(record.id) }
1036: end
1037: else
1038: options[:conditions] = if options[:conditions]
1039: selector = @association_class.build_conditions_from_options(options)
1040: @finder_sql ? lambda{|rec| selector[rec] && @finder_sql[rec]} : selector
1041: elsif @finder_sql
1042: @finder_sql
1043: end
1044:
1045: options[:readonly] ||= false
1046: options[:order] ||= @options[:order]
1047:
1048: join_records = @owner.connection.db.get_table(@join_table.to_sym).select(&@join_sql)
1049: association_ids = join_records.map { |rec| rec.send @association_foreign_key }
1050: association_ids &= args if args.all? {|a| Integer === a }
1051: records = @association_class.find(:all, options).select{|rec| association_ids.include?(rec.id)}
1052: if args.first.kind_of?(Array)
1053: records.compact
1054: elsif records.first.nil?
1055: raise RecordNotFound
1056: else
1057: records.first
1058: end
1059: end
1060: end
[ show source ]
# File kirbybase_adapter.rb, line 972
972: def find_target
973: if @custom_finder_sql
974: join_records = @owner.connection.db.get_table(@join_table.to_sym).select do |join_record|
975: @options[:finder_sql][join_record, @owner]
976: end
977: else
978: join_records = @owner.connection.db.get_table(@join_table.to_sym).select(&@join_sql)
979: end
980: association_ids = join_records.map { |rec| rec.send @association_foreign_key }
981:
982: records = if @finder_sql
983: @association_class.find :all, :conditions => lambda{|rec| association_ids.include?(rec.recno) && @finder_sql[rec]}
984: else
985: @association_class.find :all, :conditions => lambda{|rec| association_ids.include?(rec.recno) }
986: end
987:
988: # add association properties
989: if @owner.connection.db.get_table(@join_table.to_sym).field_names.size > 3
990: join_records = join_records.inject({}){|hsh, rec| hsh[rec.send(@association_foreign_key)] = rec; hsh}
991: table = @owner.connection.db.get_table(@join_table.to_sym)
992: extras = table.field_names - [:recno, @association_foreign_key.to_sym, @association_class_primary_key_name.to_sym]
993: records.each do |rec|
994: extras.each do |field|
995: rec.send :write_attribute, field.to_s, join_records[rec.id].send(field)
996: end
997: end
998: end
999:
1000: @options[:uniq] ? uniq(records) : records
1001: end
[ show source ]
# File kirbybase_adapter.rb, line 1062
1062: def insert_record(record)
1063: if record.new_record?
1064: return false unless record.save
1065: end
1066:
1067: if @options[:insert_sql]
1068: raise ArgumentError, "SQL not supported by KirbyBase! #{@options[:insert_sql]}"
1069: @owner.connection.execute(interpolate_sql(@options[:insert_sql], record))
1070: else
1071: columns = @owner.connection.columns(@join_table, "#{@join_table} Columns")
1072:
1073: attributes = columns.inject({}) do |attributes, column|
1074: case column.name
1075: when @association_class_primary_key_name
1076: attributes[column.name] = @owner.id
1077: when @association_foreign_key
1078: attributes[column.name] = record.id
1079: else
1080: if record.attributes.has_key?(column.name)
1081: attributes[column.name] = record[column.name]
1082: end
1083: end
1084: attributes
1085: end
1086:
1087: input_rec = Hash[*@owner.send(:quoted_column_names, attributes).zip(attributes.values).flatten].symbolize_keys
1088: @owner.connection.db.get_table(@join_table.to_sym).insert(input_rec)
1089: end
1090:
1091: return true
1092: end
[ show source ]
# File kirbybase_adapter.rb, line 1003
1003: def method_missing(method, *args, &block)
1004: if @target.respond_to?(method) || (!@association_class.respond_to?(method) && Class.respond_to?(method))
1005: super
1006: else
1007: if method.to_s =~ /^find/
1008: records = @association_class.send(method, *args, &block)
1009: (records.is_a?(Array) ? records : [records]) & find_target
1010: else
1011: @association_class.send(method, *args, &block)
1012: end
1013: end
1014: end