Module: Typus::Orm::ActiveRecord::ClassMethods

Includes:
Base::ClassMethods
Defined in:
lib/typus/orm/active_record/class_methods.rb

Instance Method Summary collapse

Methods included from Base::ClassMethods

#adapter, #custom_attribute?, #read_model_config, #to_resource, #typus_actions_on, #typus_application, #typus_boolean, #typus_date_format, #typus_defaults_for, #typus_description, #typus_field_options_for, #typus_options_for, #typus_order_by, #typus_scopes, #typus_search_fields, #typus_template, #virtual_fields

Instance Method Details

#association_attribute?(field) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/typus/orm/active_record/class_methods.rb', line 74

def association_attribute?(field)
  reflect_on_association(field).macro if reflect_on_association(field)
end

#dragonfly_attribute?(field) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
# File 'lib/typus/orm/active_record/class_methods.rb', line 58

def dragonfly_attribute?(field)
  if respond_to?(:dragonfly_attachment_classes) && dragonfly_attachment_classes.map(&:attribute).include?(field)
    :dragonfly
  end
end

#get_typus_fields_for(filter) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/typus/orm/active_record/class_methods.rb', line 35

def get_typus_fields_for(filter)
  data = read_model_config['fields']
  fields = case filter.to_sym
           when :index                  then data['index'] || data['list']
           when :new, :create           then data['new'] || data['form']
           when :edit, :update, :toggle then data['edit'] || data['form']
           else
             data[filter.to_s]
           end

  fields ||= data['default'] || typus_default_fields_for(filter)
  fields = fields.extract_settings if fields.is_a?(String)
  fields.map(&:to_sym)
end

#get_typus_filtersObject



86
87
88
89
# File 'lib/typus/orm/active_record/class_methods.rb', line 86

def get_typus_filters
  data = read_model_config['filters'] || ""
  data.extract_settings.map(&:to_sym)
end

#model_fieldsObject

Model fields as an ActiveSupport::OrderedHash.



9
10
11
12
13
# File 'lib/typus/orm/active_record/class_methods.rb', line 9

def model_fields
  ActiveSupport::OrderedHash.new.tap do |hash|
    columns.map { |u| hash[u.name.to_sym] = u.type.to_sym }
  end
end

#model_relationshipsObject

Model relationships as an ActiveSupport::OrderedHash.



16
17
18
19
20
# File 'lib/typus/orm/active_record/class_methods.rb', line 16

def model_relationships
  ActiveSupport::OrderedHash.new.tap do |hash|
    reflect_on_all_associations.map { |i| hash[i.name] = i.macro }
  end
end

#paperclip_attribute?(field) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
# File 'lib/typus/orm/active_record/class_methods.rb', line 64

def paperclip_attribute?(field)
  if respond_to?(:attachment_definitions) && attachment_definitions.try(:has_key?, field)
    :paperclip
  end
end

#selector_attribute?(field) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/typus/orm/active_record/class_methods.rb', line 70

def selector_attribute?(field)
  :selector if typus_field_options_for(:selectors).include?(field)
end

#typus_default_fields_for(filter) ⇒ Object



50
51
52
# File 'lib/typus/orm/active_record/class_methods.rb', line 50

def typus_default_fields_for(filter)
  filter.to_sym.eql?(:index) ? ['id'] : model_fields.keys
end

#typus_fields_for(filter) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/typus/orm/active_record/class_methods.rb', line 22

def typus_fields_for(filter)
  ActiveSupport::OrderedHash.new.tap do |fields_with_type|
    get_typus_fields_for(filter).each do |field|
      [:virtual, :custom, :association, :selector, :dragonfly, :paperclip].each do |attribute|
        if (value = send("#{attribute}_attribute?", field))
          fields_with_type[field.to_s] = value
        end
      end
      fields_with_type[field.to_s] ||= model_fields[field]
    end
  end
end

#typus_filtersObject



78
79
80
81
82
83
84
# File 'lib/typus/orm/active_record/class_methods.rb', line 78

def typus_filters
  ActiveSupport::OrderedHash.new.tap do |fields_with_type|
    get_typus_filters.each do |field|
      fields_with_type[field.to_s] = association_attribute?(field) || model_fields[field.to_sym]
    end
  end
end

#typus_user_id?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/typus/orm/active_record/class_methods.rb', line 91

def typus_user_id?
  columns.map(&:name).include?(Typus.user_foreign_key)
end

#virtual_attribute?(field) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/typus/orm/active_record/class_methods.rb', line 54

def virtual_attribute?(field)
  :virtual if virtual_fields.include?(field.to_s)
end