Module: Typus::Orm::Base::ClassMethods

Included in:
ActiveRecord::ClassMethods, Mongoid::ClassMethods
Defined in:
lib/typus/orm/base/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#adapterObject



148
149
150
# File 'lib/typus/orm/base/class_methods.rb', line 148

def adapter
  @adapter ||= ::ActiveRecord::Base.configurations[Rails.env]['adapter']
end

#custom_attribute?(field) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
171
172
173
174
175
# File 'lib/typus/orm/base/class_methods.rb', line 168

def custom_attribute?(field)
  case field.to_s
  when 'parent', 'parent_id' then :tree
  when /password/            then :password
  when 'position'            then :position
  when /\./                  then :transversal
  end
end

#model_fieldsObject

Model fields as an ActiveSupport::OrderedHash.



7
# File 'lib/typus/orm/base/class_methods.rb', line 7

def model_fields; end

#model_relationshipsObject

Model relationships as an ActiveSupport::OrderedHash.



10
# File 'lib/typus/orm/base/class_methods.rb', line 10

def model_relationships; end

#read_model_configObject



154
155
156
# File 'lib/typus/orm/base/class_methods.rb', line 154

def read_model_config
  Typus::Configuration.config[name] or raise "No typus configuration specified for #{name}"
end

#to_resourceObject

>> Post.to_resource
=> "posts"
>> Admin::User.to_resource
=> "admin/users"

++



164
165
166
# File 'lib/typus/orm/base/class_methods.rb', line 164

def to_resource
  name.underscore.pluralize
end

#typus_actions_on(filter) ⇒ Object

Extended actions for this model on Typus.



23
24
25
26
# File 'lib/typus/orm/base/class_methods.rb', line 23

def typus_actions_on(filter)
  actions = read_model_config['actions']
  actions && actions[filter.to_s] ? actions[filter.to_s].extract_settings : []
end

#typus_applicationObject



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

def typus_application
  name = read_model_config['application'] || 'Unknown'
  name.extract_settings.first
end

#typus_boolean(attribute = :default) ⇒ Object

– Define our own boolean mappings.

Post:
  fields:
    default: title, status
    options:
      booleans:
        status: "Published", "Not published"

++



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/typus/orm/base/class_methods.rb', line 105

def typus_boolean(attribute = :default)
  options = read_model_config['fields']['options']

  boolean = if options && options['booleans'] && boolean = options['booleans'][attribute.to_s]
    boolean.is_a?(String) ? boolean.extract_settings : boolean
  else
    ["True", "False"]
  end

  [[boolean.first, "true"], [boolean.last, "false"]]
end

#typus_date_format(attribute = :default) ⇒ Object

– Custom date formats. ++



120
121
122
123
124
125
126
127
# File 'lib/typus/orm/base/class_methods.rb', line 120

def typus_date_format(attribute = :default)
  options = read_model_config['fields']['options']
  if options && options['date_formats'] && options['date_formats'][attribute.to_s]
    options['date_formats'][attribute.to_s].to_sym
  else
    :default
  end
end

#typus_defaults_for(filter) ⇒ Object

Used for search, relationships



29
30
31
# File 'lib/typus/orm/base/class_methods.rb', line 29

def typus_defaults_for(filter)
  read_model_config[filter.to_s].try(:extract_settings) || []
end

#typus_descriptionObject

Model description for admin panel.



13
14
15
# File 'lib/typus/orm/base/class_methods.rb', line 13

def typus_description
  read_model_config['description']
end

#typus_field_options_for(filter) ⇒ Object



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

def typus_field_options_for(filter)
  options = read_model_config['fields']['options']
  options && options[filter.to_s] ? options[filter.to_s].extract_settings.map(&:to_sym) : []
end

#typus_fields_for(filter) ⇒ Object

Form and list fields



18
# File 'lib/typus/orm/base/class_methods.rb', line 18

def typus_fields_for(filter); end

#typus_filtersObject



20
# File 'lib/typus/orm/base/class_methods.rb', line 20

def typus_filters; end

#typus_options_for(filter) ⇒ Object

– With Typus::Resources.setup we can define application defaults.

Typus::Resources.setup do |config|
  config.per_page = 25
end

If for any reason we need a better default for an specific resource we can override it on application.yaml.

Post:
  ...
  options:
    per_page: 25

++



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

def typus_options_for(filter)
  options = read_model_config['options']

  unless options.nil? || options[filter.to_s].nil?
    options[filter.to_s]
  else
    Typus::Resources.send(filter)
  end
end

#typus_order_byObject



88
89
90
91
92
# File 'lib/typus/orm/base/class_methods.rb', line 88

def typus_order_by
  typus_defaults_for(:order_by).map do |field|
    field.include?('-') ? "#{field.delete('-')} DESC" : "#{field} ASC"
  end.join(', ')
end

#typus_scopesObject



33
34
35
# File 'lib/typus/orm/base/class_methods.rb', line 33

def typus_scopes
  typus_defaults_for(:scopes).reject { |s| !respond_to?(s) }
end

#typus_search_fieldsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/typus/orm/base/class_methods.rb', line 37

def typus_search_fields
  Hash.new.tap do |search|
    typus_defaults_for(:search).each do |field|
      if field.starts_with?("=")
        field.slice!(0)
        search[field] = "="
      elsif field.starts_with?("^")
        field.slice!(0)
        search[field] = "^"
      else
        search[field] = "@"
      end
    end
  end
end

#typus_template(attribute) ⇒ Object

– This is user to use custome templates for attribute:

Post:
  fields:
    form: title, body, status
    options:
      templates:
        body: rich_text

Templates are stored on app/views/admin/templates. ++



141
142
143
144
145
146
# File 'lib/typus/orm/base/class_methods.rb', line 141

def typus_template(attribute)
  options = read_model_config['fields']['options']
  if options && options['templates'] && options['templates'][attribute.to_s]
    options['templates'][attribute.to_s]
  end
end

#typus_user_id?Boolean

Returns:

  • (Boolean)


152
# File 'lib/typus/orm/base/class_methods.rb', line 152

def typus_user_id?; end

#virtual_fieldsObject



177
178
179
# File 'lib/typus/orm/base/class_methods.rb', line 177

def virtual_fields
  instance_methods.map(&:to_s) - model_fields.keys.map(&:to_s)
end