Class: Faalis::Dashboard::DSL::Create

Inherits:
Base
  • Object
show all
Defined in:
lib/faalis/dashboard/dsl/create.rb

Instance Attribute Summary

Attributes inherited from Base

#action_buttons, #default_scope, #fields, #model

Instance Method Summary (collapse)

Methods inherited from Base

#action_button, #attributes, #resolve_model_reflections, #scope

Constructor Details

- (Create) initialize(model)

Returns a new instance of Create



6
7
8
9
# File 'lib/faalis/dashboard/dsl/create.rb', line 6

def initialize(model)
  super
  @field_details = {}
end

Instance Method Details

- (Object) _field_details



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/faalis/dashboard/dsl/create.rb', line 29

def _field_details
  details = @field_details.dup

  fields_for_form.each do |field|
    if !details.include? field.to_sym

      type = guess_field_type(field)
      details[field.to_sym] = initialize_field(field.to_sym, type)
    end
  end

  details
end

- (Object) attributes_properties(**options)

Specify attributes details, for example if you want to change a datetime field type to string you need to do like this:

in_form do attributes_properties date: { as: :string } end



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/faalis/dashboard/dsl/create.rb', line 17

def attributes_properties(**options)
  options_set = Set.new options.keys.map(&:to_s)

  unless options_set.subset? Set.new(@fields)
    fail "You have to provide correct attribute names in" +
         "'attributes_properties' for '#{@model}'."
  end

  # TODO: Check for valid value for each key
  @field_details = options || {}
end

- (Object) fields_for_form



43
44
45
46
47
48
49
50
51
# File 'lib/faalis/dashboard/dsl/create.rb', line 43

def fields_for_form
  fields_name = @fields.dup

  fields_name.delete('id')
  fields_name.delete('created_at')
  fields_name.delete('updated_at')

  fields_name
end

- (Object) guess_field_type(field) (private)



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/faalis/dashboard/dsl/create.rb', line 55

def guess_field_type(field)
  column = model.columns_hash[field.to_s]
  return column.type.to_sym unless column.nil?

  return :file if has_attachment? field.to_sym

  has_many                = ActiveRecord::Reflection::HasManyReflection
  has_and_belongs_to_many = ActiveRecord::Reflection::HasAndBelongsToManyReflection
  reflection              = model.reflections[field.to_s]

  unless reflection.nil?
    return nil if reflection.is_a? has_many
    return :multiple_select if reflection.is_a? has_and_belongs_to_many
    :select
  end
end

- (Boolean) has_attachment?(name) (private)

Returns:

  • (Boolean)


72
73
74
75
76
77
78
# File 'lib/faalis/dashboard/dsl/create.rb', line 72

def has_attachment?(name)
  if model.respond_to? :attachment_definitions
    return true if model.attachment_definitions.keys.include? name
  end

  false
end

- (Object) initialize_field(name, type) (private)



80
81
82
83
84
85
86
# File 'lib/faalis/dashboard/dsl/create.rb', line 80

def initialize_field(name, type)
  if respond_to?("setup_#{type}".to_sym, true)
    return send("setup_#{type}", name)
  end

  { as: type.to_sym }
end

- (Object) setup_date(name) (private)



100
101
102
103
104
# File 'lib/faalis/dashboard/dsl/create.rb', line 100

def setup_date(name)
  { as: :string,
    input_html: { class: 'datepicker1' }
  }
end

- (Object) setup_datetime(name) (private)



88
89
90
91
92
# File 'lib/faalis/dashboard/dsl/create.rb', line 88

def setup_datetime(name)
  { as: :string,
    input_html: { class: 'datetimepicker' }
  }
end

- (Object) setup_integer(name) (private)



112
113
114
# File 'lib/faalis/dashboard/dsl/create.rb', line 112

def setup_integer(name)
  { as: nil }
end

- (Object) setup_multiple_select(name) (private)



106
107
108
109
110
# File 'lib/faalis/dashboard/dsl/create.rb', line 106

def setup_multiple_select(name)
  { as: :select,
    input_html: { class: 'multiple select' }
  }
end

- (Object) setup_time(name) (private)



94
95
96
97
98
# File 'lib/faalis/dashboard/dsl/create.rb', line 94

def setup_time(name)
  { as: :string,
    input_html: { class: 'timepicker' }
  }
end