@ -17,31 +17,35 @@ describe Record do
a_record . should be_valid
end
def record_joins_expectations ( joins )
# joins == [{:domain => Squeel(:permissions, outer)}]
joins . size . should == 1
joins . first . should be_an_instance_of ( Hash )
domain_joins = joins . first [ :domain ]
domain_joins . size . should == 1
domain_joins . first [ :domain ] . _name . should == :permissions
domain_joins . first [ :domain ] . _type . should == Arel :: Nodes :: OuterJoin
end
it " queries records corectly in index " do
wheres = Record . accessible_by ( user . ability ) . where_values
joins = Record . accessible_by ( user . ability ) . joins_values
wheres . should == [ " (`permissions`.`user_id` = #{ user . id } ) OR (`domains`.`user_id` = #{ user . id } ) " ]
record_joins_expectations ( joins )
permission3
query = Record . accessible_by ( user . ability )
expected = << - SQL
SELECT ` records ` . * FROM ` records `
INNER JOIN ` domains ` ON ` domains ` . ` id ` = ` records ` . ` domain_id `
LEFT OUTER JOIN ` permissions ` ON ` permissions ` . ` domain_id ` = ` domains ` . ` id `
WHERE ( ( ( ( 1 = 0 OR
` domains ` . ` user_id ` = #{ user . id } ) OR
` permissions ` . ` user_id ` = #{ user . id } ) OR
` domains ` . ` name_reversed ` LIKE ' #{ permission3 . domain . name_reversed } .%'))
SQL
query . to_sql . should == expected . gsub ( " \n " , '' ) . gsub ( / \ s+ / , ' ' ) . strip
end
it " queries A records corectly in index " do
it " queries A records corectly in index " , :focus = > true do
permission3
query = A . accessible_by ( user . ability ( :reload = > true ) )
wheres = query . where_values
joins = query . joins_values
wheres . size . should == 2
wheres . second . should == " (`domains`.`name_reversed` = ' #{ domain3 . name_reversed } .%') OR ((`permissions`.`user_id` = #{ user . id } ) OR ((`records`.`user_id` = #{ user . id } ) OR (`domains`.`user_id` = #{ user . id } ))) "
record_joins_expectations ( joins )
expected = << - SQL
SELECT ` records ` . * FROM ` records `
INNER JOIN ` domains ` ON ` domains ` . ` id ` = ` records ` . ` domain_id `
LEFT OUTER JOIN ` permissions ` ON ` permissions ` . ` domain_id ` = ` domains ` . ` id `
WHERE ` records ` . ` type ` IN ('A') AND (((((1=0 OR
` domains ` . ` user_id ` = #{ user . id } ) OR
` records ` . ` user_id ` = #{ user . id } ) OR
` permissions ` . ` user_id ` = #{ user . id } ) OR
` domains ` . ` name_reversed ` LIKE ' #{ permission3 . domain . name_reversed } .%'))
SQL
query . to_sql . should == expected . gsub ( " \n " , '' ) . gsub ( / \ s+ / , ' ' ) . strip
end
end