Module: Faalis::Generators::Concerns::ResourceFields

Included in:
DashboardScaffold
Defined in:
lib/faalis/generators/concerns/resource_fields.rb

Overview

This concern module is one of the most important concerns in dashboard scaffold system. This module adds fields key to json file which use for defining the resource fields. fields key is an array of objects which each object represent a field. Each field have following attributes:

name: Name of field

type: Type of field. some of the most important types are: string, integer, flout, belongs_to, has_many, in, datetime. For complete list of types take a look at app/assets/javascripts/faalis/dashboard/modules/fields/

to: This attribute is just for in, has_many and belongs_to field types which define the resource that current resource have dependency with. Bear in mind that in type uses to attrbute as an array of possible value for field and will render as a combobox.

options: An object of type related objects. For example a list of current resource parents just like parent key.

bulk: All fields with true as bulk value will be used in bulk editor.

tab: ID of a Tab that this field should be appear on.

required: Field will be non optional.

Instance Method Summary (collapse)

Instance Method Details

- (Object) attrs(field_name, attr_name) (private)

Return the value of attr_name of field_name in resource_data["fields"]



58
59
60
61
62
63
64
# File 'lib/faalis/generators/concerns/resource_fields.rb', line 58

def attrs(field_name, attr_name)
  if fields?
    field = fields_with('name', field_name)[0]
    return field[attr_name]
  end
  nil
end

- (Object) fields(fields_source = resource_data['fields']) (private)

An array of fields like [name, type]



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/faalis/generators/concerns/resource_fields.rb', line 38

def fields(fields_source = resource_data['fields'])
  fields = []
  relations = ['belongs_to', 'has_many']

  if fields?
    fields_source.each do |field|
      name = field['name']
      type = field['type']
      to = field['to']
      options = field['options'] || {}

      type = Relation.new(type, to, options) if relations.include? type
      fields << [name, type, to]
    end
  end
  fields
end

- (Boolean) fields? (private)

Returns:

  • (Boolean)


138
139
140
141
142
143
# File 'lib/faalis/generators/concerns/resource_fields.rb', line 138

def fields?
  if resource_data.include? 'fields'
    return true unless resource_data['fields'].nil?
  end
  false
end

- (Object) fields_as_params(relations = false) (private)

Return an string to use as a function parameters each field appears as symbol



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/faalis/generators/concerns/resource_fields.rb', line 76

def fields_as_params(relations = false)
  # FIXME: cyclomatic complexity is to high
  result = ''
  field_num = 0

  fields.each do |name, type|
    if relations
      if ['belongs_to'].include? type
        result += " :#{name}_id"
      else
        result += " :#{name}"
      end

      field_num += 1
      result += ',' if field_num < fields.length

    else
      unless ['belongs_to', 'has_many'].include? type
        result += " :#{name}"
        field_num += 1
        result += ',' if field_num < fields.length
      end
    end
  end

  if result
    result = ",#{result}"
    result = result[0..-2] if result[-1] == ','
  end

  result
end

- (Object) fields_hash (private)



66
67
68
# File 'lib/faalis/generators/concerns/resource_fields.rb', line 66

def fields_hash
  Hash[fields]
end

- (Object) fields_with(attr, value) (private)



113
114
115
116
117
118
119
120
121
# File 'lib/faalis/generators/concerns/resource_fields.rb', line 113

def fields_with(attr, value)
  raw_fields_data.select do |f|
    if f.include? attr
      f[attr] == value ? true : false
    else
      false
    end
  end
end

- (Object) fields_with_attribute(attr) (private)



123
124
125
126
127
128
# File 'lib/faalis/generators/concerns/resource_fields.rb', line 123

def fields_with_attribute(attr)
  field_list = raw_fields_data.select do |f|
    f.include?(attr) ? true : false
  end
  field_list
end

- (Object) grid_fields (private)



70
71
72
# File 'lib/faalis/generators/concerns/resource_fields.rb', line 70

def grid_fields
  fields_with("view_in_grid", true)
end

- (Boolean) no_duplicate? (private)

Returns:

  • (Boolean)


134
135
136
# File 'lib/faalis/generators/concerns/resource_fields.rb', line 134

def no_duplicate?
  resource_data.include?('no_duplicate') &&  resource_data['no_duplicate']
end

- (Boolean) no_filter? (private)

Returns:

  • (Boolean)


130
131
132
# File 'lib/faalis/generators/concerns/resource_fields.rb', line 130

def no_filter?
  resource_data.include?('no_filter') &&  resource_data['no_filter']
end

- (Object) raw_fields_data (private)



109
110
111
# File 'lib/faalis/generators/concerns/resource_fields.rb', line 109

def raw_fields_data
  resource_data['fields'] || []
end