Class: Carnival::Field

Inherits:
Object
  • Object
show all
Defined in:
app/models/carnival/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, params = {}) ⇒ Field

Returns a new instance of Field.



5
6
7
8
9
10
# File 'app/models/carnival/field.rb', line 5

def initialize(name, params={})
  @params = params
  @name = name
  set_position_by_params
  validate
end

Instance Attribute Details

#columnObject

Returns the value of attribute column.



3
4
5
# File 'app/models/carnival/field.rb', line 3

def column
  @column
end

#lineObject

Returns the value of attribute line.



3
4
5
# File 'app/models/carnival/field.rb', line 3

def line
  @line
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'app/models/carnival/field.rb', line 3

def name
  @name
end

#paramsObject

Returns the value of attribute params.



3
4
5
# File 'app/models/carnival/field.rb', line 3

def params
  @params
end

#sizeObject

Returns the value of attribute size.



3
4
5
# File 'app/models/carnival/field.rb', line 3

def size
  @size
end

Instance Method Details

#actionsObject



145
146
147
# File 'app/models/carnival/field.rb', line 145

def actions
  @params[:actions]
end

#add_empty_optionObject



178
179
180
# File 'app/models/carnival/field.rb', line 178

def add_empty_option
  @params[:add_empty_option].present? && @params[:add_empty_option]
end

#advanced_search_operatorObject



140
141
142
143
# File 'app/models/carnival/field.rb', line 140

def advanced_search_operator
  return @params[:advanced_search][:operator] if advanced_searchable? and @params[:advanced_search][:operator].present?
  :like
end

#advanced_searchable?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'app/models/carnival/field.rb', line 132

def advanced_searchable?
  @params[:advanced_search]
end

#asObject



154
155
156
# File 'app/models/carnival/field.rb', line 154

def as
  @params[:as]
end

#association_field_nameObject



30
31
32
33
34
# File 'app/models/carnival/field.rb', line 30

def association_field_name
  if specified_association?
    get_association_and_field[:field]
  end
end

#association_nameObject



26
27
28
# File 'app/models/carnival/field.rb', line 26

def association_name
  get_association_and_field[:association] || @name
end

#css_classObject



40
41
42
43
44
45
46
# File 'app/models/carnival/field.rb', line 40

def css_class
  if @params[:css_class]
    return @params[:css_class]
  else
    return ""
  end
end

#date_filter?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'app/models/carnival/field.rb', line 48

def date_filter?
  @params[:date_filter]
end

#date_filter_periodsObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/carnival/field.rb', line 60

def date_filter_periods
  if @params[:date_filter_periods]
    @params[:date_filter_periods]
  else
    {:today => ["#{Date.today}", "#{Date.today}"],
     :yesterday => ["#{Date.today - 1.day}", "#{Date.today - 1.day}"],
     :this_week => ["#{Date.today.beginning_of_week}", "#{Date.today.end_of_week}"],
     :last_week => ["#{(Date.today - 1.week).beginning_of_week}", "#{(Date.today - 1.week).end_of_week}"],
     :this_month => ["#{Date.today.beginning_of_month}", "#{Date.today.end_of_month}"],
     :last_month => ["#{(Date.today - 1.month).beginning_of_month}", "#{(Date.today - 1.month).end_of_month}"],
     :this_year => ["#{Date.today.beginning_of_year}", "#{Date.today.end_of_year}"],
     :last_year => ["#{(Date.today - 1.year).beginning_of_year}", "#{(Date.today - 1.year).end_of_year}"]
    }
  end
end

#default_date_filterObject



52
53
54
55
56
57
58
# File 'app/models/carnival/field.rb', line 52

def default_date_filter
  if @params[:date_filter_default]
    @params[:date_filter_default]
  else
    date_filter_periods.first.first
  end
end

#default_sort_directionObject



80
81
82
83
84
85
86
87
# File 'app/models/carnival/field.rb', line 80

def default_sort_direction
  if default_sortable?
    if @params[:sortable][:direction]
      return @params[:sortable][:direction]
    end
  end
  return :asc
end

#default_sortable?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'app/models/carnival/field.rb', line 76

def default_sortable?
  @params[:sortable] && @params[:sortable].class == Hash && @params[:sortable][:default] == true
end

#depends_onObject



89
90
91
# File 'app/models/carnival/field.rb', line 89

def depends_on
  @params[:depends_on]
end

#hidden?(record, controller) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
# File 'app/models/carnival/field.rb', line 12

def hidden?(record, controller)
  if @params.has_key?(:hide_if)
    block = @params[:hide_if]
    controller.instance_variable_set(:@record, record)
    controller.instance_eval(&block)
  else
    false
  end
end

#name_for_translationObject



36
37
38
# File 'app/models/carnival/field.rb', line 36

def name_for_translation
  @name.to_s.gsub('.', '_')
end

#nested_form?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'app/models/carnival/field.rb', line 93

def nested_form?
  @params[:nested_form]
end

#nested_form_allow_destroy?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'app/models/carnival/field.rb', line 97

def nested_form_allow_destroy?
  @params[:nested_form_allow_destroy]
end

#nested_form_modes?(mode) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
104
105
106
# File 'app/models/carnival/field.rb', line 101

def nested_form_modes? (mode)
  associate = get_associate_nested_form_mode
  return true if associate.present? && mode == :associate
  return @params[:nested_form_modes].include?(mode) unless @params[:nested_form_modes].nil?
  return false
end

#nested_form_scopeObject



108
109
110
111
112
113
# File 'app/models/carnival/field.rb', line 108

def nested_form_scope
  return nil if !nested_form_modes? :associate
  associate_mode =  get_associate_nested_form_mode
  return nil if associate_mode.is_a? Symbol
  return associate_mode[:scope] if associate_mode[:scope].present?
end

#partial_nameObject



174
175
176
# File 'app/models/carnival/field.rb', line 174

def partial_name
  @params[:partial_name] || name.to_s
end

#presenter_classObject



170
171
172
# File 'app/models/carnival/field.rb', line 170

def presenter_class
  @params[:presenter]
end

#searchable?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'app/models/carnival/field.rb', line 128

def searchable?
  @params[:searchable]
end

#show_as_listObject



136
137
138
# File 'app/models/carnival/field.rb', line 136

def show_as_list
  @params[:show_as_list]
end

#show_viewObject



162
163
164
# File 'app/models/carnival/field.rb', line 162

def show_view
  @params[:show_view]
end

#sort_nameObject



166
167
168
# File 'app/models/carnival/field.rb', line 166

def sort_name
  @name.to_s
end

#sortable?Boolean

Returns:

  • (Boolean)


115
116
117
118
119
120
# File 'app/models/carnival/field.rb', line 115

def sortable?
  return true if @params[:sortable].nil?
  return true if @params[:sortable]
  return true if @params[:sortable].class == Hash
  return false
end

#sortable_paramsObject



122
123
124
125
126
# File 'app/models/carnival/field.rb', line 122

def sortable_params
  return false if !sortable?
  return @params[:sortable].to_json if @params[:sortable].class == Hash
  return true
end

#specified_association?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'app/models/carnival/field.rb', line 22

def specified_association?
  not get_association_and_field[:association].nil?
end

#valid_for_action?(action) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
152
# File 'app/models/carnival/field.rb', line 149

def valid_for_action?(action)
  return false if @params[:actions].nil?
  @params[:actions].include?(action)
end

#widgetObject



158
159
160
# File 'app/models/carnival/field.rb', line 158

def widget
  @params[:widget].present? ? @params[:widget] : :input
end