Class: Bhf::Platform::Base

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Base

Returns a new instance of Base.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bhf/platform/base.rb', line 5

def initialize(options)
  @objects = []

  @name = options.name
  @data = options.hash
  @settings = options.settings_base
  @user = @settings.user

  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)

  @page_name = options.page_name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#objectsObject (readonly)

Returns the value of attribute objects.



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

def objects
  @objects
end

#page_nameObject (readonly)

Returns the value of attribute page_name.



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

def page_name
  @page_name
end

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

#title_singularObject (readonly)

Returns the value of attribute title_singular.



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

def title_singular
  @title_singular
end

#title_zeroObject (readonly)

Returns the value of attribute title_zero.



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

def title_zero
  @title_zero
end

Instance Method Details

#columnsObject



93
94
95
96
97
98
99
100
101
102
# File 'lib/bhf/platform/base.rb', line 93

def columns
  return @columns if @columns

  tmp = default_attrs(table_columns, attributes[0..5])
  if sortable and ! table_columns
    tmp = remove_excludes(tmp, [sortable_property.to_s])
  end

  @columns = remove_excludes(tmp, table_value(:exclude))
end

#columns_countObject



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

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

#custom_columns?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/bhf/platform/base.rb', line 145

def custom_columns?
  table_columns.is_a?(Array)
end


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

def custom_footer
  table_value(:custom_footer)
end


199
200
201
# File 'lib/bhf/platform/base.rb', line 199

def custom_link
  table_value 'custom_link'
end

#custom_partialObject



207
208
209
# File 'lib/bhf/platform/base.rb', line 207

def custom_partial
  table_value 'partial'
end

#custom_searchObject



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

def custom_search
  table_value(:custom_search)
end

#data_sourceObject



215
216
217
218
219
220
# File 'lib/bhf/platform/base.rb', line 215

def data_source
  temp_scope = read_data_source
  return unless temp_scope
  return temp_scope[0] if temp_scope.is_a?(Array)
  temp_scope
end

#definitionsObject



104
105
106
107
108
109
# File 'lib/bhf/platform/base.rb', line 104

def definitions
  return @definitions if @definitions

  tmp = default_attrs(show_value(:display) || show_value(:definitions), attributes)
  @definitions = remove_excludes(tmp, show_value(:exclude))
end

#entries_per_pageObject



237
238
239
# File 'lib/bhf/platform/base.rb', line 237

def entries_per_page
  table_value(:per_page)
end

#fieldsObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/bhf/platform/base.rb', line 82

def fields
  return @fields if @fields

  tmp = default_attrs(form_value(:display), attributes)
  if sortable and ! form_value(:display)
    tmp = remove_excludes(tmp, [sortable_property.to_s])
  end

  @fields = remove_excludes(tmp, form_value(:exclude))
end

#formObject



161
162
163
# File 'lib/bhf/platform/base.rb', line 161

def form
  @data['form']
end

#get_objects(options = {}, paginate_options = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/bhf/platform/base.rb', line 29

def get_objects(options = {}, paginate_options = nil)
  if user_scope? and @user
    chain = @user.send(table_value(:user_scope).to_sym)
  else
    chain = model
    if options[:scope].present?
      chain = chain.send options[:scope]
    elsif data_source
      chain = chain.send data_source
    end
  end

  if options[:order].present? and options[:direction].present?
    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

#has_file_upload?Boolean

Returns:

  • (Boolean)


111
112
113
114
115
116
117
118
# File 'lib/bhf/platform/base.rb', line 111

def has_file_upload?
  return true if form_value(:multipart) == true

  fields.each do |field|
    return true if field.form_type == :file
  end
  false
end

#hide_createObject



187
188
189
# File 'lib/bhf/platform/base.rb', line 187

def hide_create
  table_value('hide_create') || table_value('hide_new') || table_value('hide_add')
end

#hide_deleteObject



195
196
197
# File 'lib/bhf/platform/base.rb', line 195

def hide_delete
  table_value('hide_delete') || table_value('hide_destroy')
end

#hide_editObject



183
184
185
# File 'lib/bhf/platform/base.rb', line 183

def hide_edit
  table_value 'hide_edit'
end

#hooks(method) ⇒ Object



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

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

#modelObject



58
59
60
61
62
63
64
# File 'lib/bhf/platform/base.rb', line 58

def model
  @model ||= if @data['model']
    @data['model']
  else
    @name
  end.classify.constantize
end

#model_nameObject



66
67
68
# File 'lib/bhf/platform/base.rb', line 66

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

#paginationObject



25
26
27
# File 'lib/bhf/platform/base.rb', line 25

def pagination
  @pagination ||= Bhf::Platform::Pagination.new(entries_per_page)
end

#read_data_sourceObject



211
212
213
# File 'lib/bhf/platform/base.rb', line 211

def read_data_source
  table_value(:source) || table_value(:scope)
end

#remove_excludes(attrs, excludes) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/bhf/platform/base.rb', line 74

def remove_excludes(attrs, excludes)
  return attrs unless excludes
  attrs.each_with_object([]) do |attribute, obj|
    next if excludes.include?(attribute.name.to_s)
    obj << attribute
  end
end

#scopesObject



222
223
224
225
226
227
228
229
230
231
# File 'lib/bhf/platform/base.rb', line 222

def scopes
  temp_scope = read_data_source
  return unless temp_scope.is_a?(Array) and temp_scope.count > 1
  temp_scope.each_with_object([]) do |s, array|
    array << OpenStruct.new(
      name: I18n.t("bhf.platforms.#{@name}.scopes.#{s}", default: s),
      value: s
    )
  end
end

#search?Boolean

Returns:

  • (Boolean)


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

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

#search_field?Boolean

Returns:

  • (Boolean)


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

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

#showObject



157
158
159
# File 'lib/bhf/platform/base.rb', line 157

def show
  @data['show']
end

#show_duplicateObject



191
192
193
# File 'lib/bhf/platform/base.rb', line 191

def show_duplicate
  table_value 'show_duplicate'
end

#show_extra_fieldsObject



233
234
235
# File 'lib/bhf/platform/base.rb', line 233

def show_extra_fields
  show_value(:extra_fields)
end

#sortableObject



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

def sortable
  table_value 'sortable'
end

#sortable_propertyObject



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

def sortable_property
  (@data['sortable_property'] || :position).to_sym
end

#tableObject



153
154
155
# File 'lib/bhf/platform/base.rb', line 153

def table
  @data['table']
end

#table_columnsObject



141
142
143
# File 'lib/bhf/platform/base.rb', line 141

def table_columns
  table_value(:display) || table_value(:columns)
end

#table_hide?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/bhf/platform/base.rb', line 70

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

#table_quick_editObject



203
204
205
# File 'lib/bhf/platform/base.rb', line 203

def table_quick_edit
  table_value 'quick_edit'
end

#user_scope?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/bhf/platform/base.rb', line 149

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