File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11# frozen_string_literal: true
22
3- if defined? ( ::ActiveRecord )
4- ::ActiveRecord ::Associations ::Builder ::HasOne . class_eval do
5- # Based on
6- # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/builder/collection_association.rb#L50
7- # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/builder/singular_association.rb#L11
8- def self . define_accessors ( mixin , reflection )
9- super
10- name = reflection . name
11- mixin . class_eval <<-CODE , __FILE__ , __LINE__ + 1
12- def #{ name } _id
13- # if an attribute is already defined with this methods name we should just use it
14- return read_attribute(__method__) if has_attribute?(__method__)
15- association(:#{ name } ).reader.try(:id)
16- end
17- CODE
18- end
3+ ::ActiveRecord ::Associations ::Builder ::HasOne . class_eval do
4+ # Based on
5+ # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/builder/collection_association.rb#L50
6+ # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/builder/singular_association.rb#L11
7+ def self . define_accessors ( mixin , reflection )
8+ super
9+ name = reflection . name
10+ mixin . class_eval <<-CODE , __FILE__ , __LINE__ + 1
11+ def #{ name } _id
12+ # if an attribute is already defined with this methods name we should just use it
13+ return read_attribute(__method__) if has_attribute?(__method__)
14+ association(:#{ name } ).reader.try(:id)
15+ end
16+ CODE
1917 end
2018end
Original file line number Diff line number Diff line change 22
33module FastJsonapi
44 require 'fast_jsonapi/object_serializer'
5- require 'extensions/has_one'
5+ if defined? ( ::Rails )
6+ require 'fast_jsonapi/railtie'
7+ elsif defined? ( ::ActiveRecord )
8+ require 'extensions/has_one'
9+ end
610end
Original file line number Diff line number Diff line change 1+ # frozen_string_literal: true
2+
3+ require 'rails/railtie'
4+
5+ class Railtie < Rails ::Railtie
6+ initializer 'fast_jsonapi.active_record' do
7+ ActiveSupport . on_load :active_record do
8+ require 'extensions/has_one'
9+ end
10+ end
11+ end
You can’t perform that action at this time.
0 commit comments