Module: Cookbook::Mixins::ActsAsUseOf::ClassMethods

Defined in:
lib/cookbook/mixins/acts_as_use_of.rb

Overview

Extended by has_cookbook mixin

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#used_inObject

Returns the value of attribute used_in.



40
41
42
# File 'lib/cookbook/mixins/acts_as_use_of.rb', line 40

def used_in
  @used_in
end

Instance Method Details

#associate_used_inObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cookbook/mixins/acts_as_use_of.rb', line 42

def associate_used_in
  tables = used_in
  tables.each do |table_sym|
    model = table_sym.to_s.classify.constantize
    name = model.model_name.to_s
    uses_symbol = "#{model.model_name.param_key}_uses".to_sym
    singular_symbol = table_sym.to_s.singularize.to_sym

    has_many uses_symbol, lambda {
      where(use_of_type: name)
    }, as: :use_in, class_name: 'Cookbook::Use', inverse_of: singular_symbol

    # Used only as a getter, not a setter
    accepts_nested_attributes_for uses_symbol, reject_if: :all_blank, allow_destroy: true

    has_many table_sym, through: uses_symbol, source: :use_of, source_type: name

    if defined?(RailsAdmin)
      rails_admin do
        include_all_fields
        field :uses do
          visible false
        end
        tables.each do |table_sym|
          singular = table_sym.to_s.singularize
          uses_symbol = "#{singular}_uses".to_sym
          field uses_symbol do # Show only on reload
            visible do
              !bindings[:object].new_record?
            end
          end
          field table_sym do # We don't want these associations to show
            visible false
          end
        end
      end
    end
  end
end