Class: Bhf::Platform

Inherits:
Object
  • Object
show all
Defined in:
lib/bhf/platform.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, page_name, user = nil) ⇒ Platform

Returns a new instance of Platform.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bhf/platform.rb', line 7

def initialize(options, page_name, user = nil)
  @objects = []

  if options.is_a?(String)
    options = {options => nil}
  end
  @name = options.keys[0]
  @data = options.values[0] || {}
  @collection = get_collection

  t_model_path = "activerecord.models.#{model.model_name.to_s.downcase}"
  model_name = I18n.t(t_model_path, count: 2, default: @name.pluralize.capitalize)
  @title = I18n.t("bhf.platforms.#{@name}.title", count: 2, default: model_name)
  model_name = I18n.t(t_model_path, count: 1, default: @name.singularize.capitalize)
  @title_singular = I18n.t("bhf.platforms.#{@name}.title", count: 1, default: model_name)
  model_name = I18n.t(t_model_path, count: 0, default: @name.singularize.capitalize)
  @title_zero = I18n.t("bhf.platforms.#{@name}.title", count: 0, default: model_name)

  @model = model
  @page_name = page_name
  @user = user
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/bhf/platform.rb', line 5

def name
  @name
end

#objectsObject (readonly)

Returns the value of attribute objects.



5
6
7
# File 'lib/bhf/platform.rb', line 5

def objects
  @objects
end

#page_nameObject (readonly)

Returns the value of attribute page_name.



5
6
7
# File 'lib/bhf/platform.rb', line 5

def page_name
  @page_name
end

#paginationObject

Returns the value of attribute pagination.



4
5
6
# File 'lib/bhf/platform.rb', line 4

def pagination
  @pagination
end

#titleObject (readonly)

Returns the value of attribute title.



5
6
7
# File 'lib/bhf/platform.rb', line 5

def title
  @title
end

#title_singularObject (readonly)

Returns the value of attribute title_singular.



5
6
7
# File 'lib/bhf/platform.rb', line 5

def title_singular
  @title_singular
end

#title_zeroObject (readonly)

Returns the value of attribute title_zero.



5
6
7
# File 'lib/bhf/platform.rb', line 5

def title_zero
  @title_zero
end

Instance Method Details

#columnsObject



94
95
96
97
98
99
# File 'lib/bhf/platform.rb', line 94

def columns
  default_attrs(table_options(:display) || table_options(:columns), @collection[0..5]).
  each_with_object([]) do |field, obj|
    obj << Bhf::Data::Column.new(field)
  end
end

#columns_countObject



143
144
145
# File 'lib/bhf/platform.rb', line 143

def columns_count
  columns.count + (sortable ? 2 : 1)
end

#custom_columns?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/bhf/platform.rb', line 46

def custom_columns?
  table_options(:columns).is_a?(Array)
end


171
172
173
# File 'lib/bhf/platform.rb', line 171

def custom_link
  table_options 'custom_link'
end

#custom_partialObject



175
176
177
# File 'lib/bhf/platform.rb', line 175

def custom_partial
  table_options 'partial'
end

#custom_searchObject



42
43
44
# File 'lib/bhf/platform.rb', line 42

def custom_search
  table_options(:custom_search)
end

#definitionsObject



101
102
103
104
105
106
# File 'lib/bhf/platform.rb', line 101

def definitions
  default_attrs(show_options(:display) || show_options(:definitions), @collection, false).
  each_with_object([]) do |field, obj|
    obj << Bhf::Data::Show.new(field)
  end
end

#entries_per_pageObject



112
113
114
# File 'lib/bhf/platform.rb', line 112

def entries_per_page
  table_options(:per_page)
end

#fieldsObject



90
91
92
# File 'lib/bhf/platform.rb', line 90

def fields
  default_attrs(form_options(:display), @collection, false)
end

#formObject



133
134
135
# File 'lib/bhf/platform.rb', line 133

def form
  @data['form']
end

#has_file_upload?Boolean

Returns:

  • (Boolean)


116
117
118
119
120
121
122
123
# File 'lib/bhf/platform.rb', line 116

def has_file_upload?
  return true if form_options(:multipart) == true
  
  fields.each do |field|
    return true if field.form_type.to_sym == :file
  end
  false
end

#hide_createObject



159
160
161
# File 'lib/bhf/platform.rb', line 159

def hide_create
  table_options('hide_create') || table_options('hide_new')
end

#hide_deleteObject



167
168
169
# File 'lib/bhf/platform.rb', line 167

def hide_delete
  table_options 'hide_delete'
end

#hide_editObject



155
156
157
# File 'lib/bhf/platform.rb', line 155

def hide_edit
  table_options 'hide_edit'
end

#hooks(method) ⇒ Object



137
138
139
140
141
# File 'lib/bhf/platform.rb', line 137

def hooks(method)
  if @data['hooks'] && @data['hooks'][method.to_s]
    @data['hooks'][method.to_s].to_sym
  end
end

#modelObject



80
81
82
83
84
# File 'lib/bhf/platform.rb', line 80

def model
  return @model if @model
  return @data['model'].constantize if @data['model']
  @name.singularize.camelize.constantize
end

#model_nameObject



86
87
88
# File 'lib/bhf/platform.rb', line 86

def model_name
  ActiveModel::Naming.singular(model)
end

#prepare_objects(options, paginate_options = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/bhf/platform.rb', line 54

def prepare_objects(options, paginate_options = nil)
  if user_scope?
    chain = @user.send(table_options(:user_scope).to_sym)
  else
    chain = model
    chain = chain.send data_source if data_source
  end

  unless options[:order].blank?
    chain = chain.reorder("#{options[:order]} #{options[:direction]}")
  end

  if search? && options[:search].present?
    chain = do_search(chain, options[:search])
  end


  if paginate_options && !sortable
    chain = chain.page(paginate_options[:page]).per(paginate_options[:per_page])
  elsif chain == model
    chain = chain.all
  end

  @objects = chain
end

#return_toObject



179
180
181
# File 'lib/bhf/platform.rb', line 179

def return_to
  form_options 'return_to'
end

#search?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/bhf/platform.rb', line 30

def search?
  table_options(:search) != false
end

#search_field?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/bhf/platform.rb', line 38

def search_field?
  table_options(:search_field) != false
end

#showObject



129
130
131
# File 'lib/bhf/platform.rb', line 129

def show
  @data['show']
end

#show_duplicateObject



163
164
165
# File 'lib/bhf/platform.rb', line 163

def show_duplicate
  table_options 'show_duplicate'
end

#show_extra_fieldsObject



108
109
110
# File 'lib/bhf/platform.rb', line 108

def show_extra_fields
  show_options(:extra_fields)
end

#sortableObject



147
148
149
# File 'lib/bhf/platform.rb', line 147

def sortable
  table_options 'sortable'
end

#sortable_propertyObject



151
152
153
# File 'lib/bhf/platform.rb', line 151

def sortable_property
  table_options 'sortable_property'
end

#tableObject



125
126
127
# File 'lib/bhf/platform.rb', line 125

def table
  @data['table']
end

#table_hide?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/bhf/platform.rb', line 34

def table_hide?
  table_options(:hide) == true || model.bhf_embedded?
end

#user_scope?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/bhf/platform.rb', line 50

def user_scope?
  @user && table_options(:user_scope)
end