rom-sql 0.6.1 → 0.7.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -5
  3. data/.rubocop_todo.yml +12 -6
  4. data/.travis.yml +3 -2
  5. data/CHANGELOG.md +27 -0
  6. data/Gemfile +1 -0
  7. data/lib/rom/plugins/relation/sql/auto_combine.rb +45 -0
  8. data/lib/rom/plugins/relation/sql/auto_wrap.rb +48 -0
  9. data/lib/rom/plugins/relation/sql/base_view.rb +31 -0
  10. data/lib/rom/sql.rb +17 -10
  11. data/lib/rom/sql/commands/error_wrapper.rb +1 -1
  12. data/lib/rom/sql/commands/update.rb +1 -1
  13. data/lib/rom/sql/gateway.rb +8 -2
  14. data/lib/rom/sql/header.rb +1 -1
  15. data/lib/rom/sql/migration.rb +11 -20
  16. data/lib/rom/sql/migration/migrator.rb +4 -0
  17. data/lib/rom/sql/{relation/associations.rb → plugin/assoc_macros.rb} +17 -2
  18. data/lib/rom/sql/plugin/assoc_macros/class_interface.rb +128 -0
  19. data/lib/rom/sql/plugin/associates.rb +2 -4
  20. data/lib/rom/sql/plugin/pagination.rb +1 -1
  21. data/lib/rom/sql/plugins.rb +4 -2
  22. data/lib/rom/sql/relation.rb +46 -6
  23. data/lib/rom/sql/relation/reading.rb +63 -0
  24. data/lib/rom/sql/tasks/migration_tasks.rake +37 -16
  25. data/lib/rom/sql/version.rb +1 -1
  26. data/rom-sql.gemspec +2 -2
  27. data/spec/fixtures/migrations/20150403090603_create_carrots.rb +1 -1
  28. data/spec/integration/combine_spec.rb +6 -6
  29. data/spec/integration/commands/create_spec.rb +25 -25
  30. data/spec/integration/commands/delete_spec.rb +6 -6
  31. data/spec/integration/commands/update_spec.rb +5 -5
  32. data/spec/integration/{repository_spec.rb → gateway_spec.rb} +34 -21
  33. data/spec/integration/migration_spec.rb +3 -3
  34. data/spec/integration/read_spec.rb +13 -7
  35. data/spec/shared/database_setup.rb +3 -5
  36. data/spec/spec_helper.rb +3 -6
  37. data/spec/support/active_support_notifications_spec.rb +1 -1
  38. data/spec/support/rails_log_subscriber_spec.rb +1 -1
  39. data/spec/unit/association_errors_spec.rb +4 -3
  40. data/spec/unit/combined_associations_spec.rb +7 -5
  41. data/spec/unit/gateway_spec.rb +2 -2
  42. data/spec/unit/logger_spec.rb +1 -1
  43. data/spec/unit/many_to_many_spec.rb +7 -4
  44. data/spec/unit/many_to_one_spec.rb +14 -8
  45. data/spec/unit/migration_tasks_spec.rb +7 -6
  46. data/spec/unit/one_to_many_spec.rb +16 -10
  47. data/spec/unit/plugin/base_view_spec.rb +18 -0
  48. data/spec/unit/plugin/pagination_spec.rb +10 -10
  49. data/spec/unit/relation_spec.rb +69 -3
  50. data/spec/unit/schema_spec.rb +5 -3
  51. metadata +19 -26
  52. data/lib/rom/sql/relation/class_methods.rb +0 -116
  53. data/lib/rom/sql/relation/inspection.rb +0 -16
@@ -1,116 +0,0 @@
1
- module ROM
2
- module SQL
3
- class Relation < ROM::Relation
4
- # Class DSL for SQL relations
5
- #
6
- # @api private
7
- module ClassMethods
8
- # Set up model and association ivars for descendant class
9
- #
10
- # @api private
11
- def inherited(klass)
12
- klass.class_eval do
13
- class << self
14
- attr_reader :model, :associations
15
- end
16
- end
17
- klass.instance_variable_set('@model', Class.new(Sequel::Model))
18
- klass.instance_variable_set('@associations', [])
19
- super
20
- end
21
-
22
- # Set up a one-to-many association
23
- #
24
- # @example
25
- # class Users < ROM::Relation[:sql]
26
- # one_to_many :tasks, key: :user_id
27
- #
28
- # def with_tasks
29
- # association_join(:tasks)
30
- # end
31
- # end
32
- #
33
- # @param [Symbol] name The name of the association
34
- # @param [Hash] options The options hash
35
- # @option options [Symbol] :key Name of the key to join on
36
- # @option options [Hash] :on Additional conditions for join
37
- # @option options [Hash] :conditions Additional conditions for WHERE
38
- #
39
- # @api public
40
- def one_to_many(name, options)
41
- associations << [__method__, name, { relation: name }.merge(options)]
42
- end
43
-
44
- # Set up a many-to-many association
45
- #
46
- # @example
47
- # class Tasks < ROM::Relation[:sql]
48
- # many_to_many :tags,
49
- # join_table: :task_tags,
50
- # left_key: :task_id,
51
- # right_key: :tag_id,
52
- #
53
- # def with_tags
54
- # association_join(:tags)
55
- # end
56
- # end
57
- #
58
- # @param [Symbol] name The name of the association
59
- # @param [Hash] options The options hash
60
- # @option options [Symbol] :join_table Name of the join table
61
- # @option options [Hash] :left_key Name of the left join key
62
- # @option options [Hash] :right_key Name of the right join key
63
- # @option options [Hash] :on Additional conditions for join
64
- # @option options [Hash] :conditions Additional conditions for WHERE
65
- #
66
- # @api public
67
- def many_to_many(name, options = {})
68
- associations << [__method__, name, { relation: name }.merge(options)]
69
- end
70
-
71
- # Set up a many-to-one association
72
- #
73
- # @example
74
- # class Tasks < ROM::Relation[:sql]
75
- # many_to_one :users, key: :user_id
76
- #
77
- # def with_users
78
- # association_join(:users)
79
- # end
80
- # end
81
- #
82
- # @param [Symbol] name The name of the association
83
- # @param [Hash] options The options hash
84
- # @option options [Symbol] :join_table Name of the join table
85
- # @option options [Hash] :key Name of the join key
86
- # @option options [Hash] :on Additional conditions for join
87
- # @option options [Hash] :conditions Additional conditions for WHERE
88
- #
89
- # @api public
90
- def many_to_one(name, options = {})
91
- associations << [__method__, name, { relation: name }.merge(options)]
92
- end
93
-
94
- # Finalize the relation by setting up its associations (if any)
95
- #
96
- # @api private
97
- def finalize(relations, relation)
98
- return unless relation.dataset.db.table_exists?(dataset)
99
-
100
- model.set_dataset(relation.dataset)
101
- model.dataset.naked!
102
-
103
- associations.each do |*args, assoc_opts|
104
- options = Hash[assoc_opts]
105
- other = relations[options.delete(:relation) || args[1]].model
106
- model.public_send(*args, options.merge(class: other))
107
- end
108
-
109
- model.freeze
110
-
111
- super
112
- end
113
- end
114
- end
115
- end
116
- end
@@ -1,16 +0,0 @@
1
- module ROM
2
- module SQL
3
- class Relation < ROM::Relation
4
- module Inspection
5
- def model
6
- self.class.model
7
- end
8
-
9
- # @api private
10
- def primary_key
11
- model.primary_key
12
- end
13
- end
14
- end
15
- end
16
- end