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



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

def columns
  return @columns if @columns
  
  tmp = default_attrs(table_columns, attributes[0..5], true)
  if sortable and ! table_columns
    tmp = remove_excludes(tmp, [sortable_property.to_s])
  end
    
  @columns = remove_excludes(tmp, table_value(:exclude))
end

#columns_countObject



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

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

#custom_columns?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/bhf/platform/base.rb', line 131

def custom_columns?
  table_columns.is_a?(Array)
end


185
186
187
# File 'lib/bhf/platform/base.rb', line 185

def custom_link
  table_value 'custom_link'
end

#custom_partialObject



193
194
195
# File 'lib/bhf/platform/base.rb', line 193

def custom_partial
  table_value 'partial'
end

#custom_searchObject



123
124
125
# File 'lib/bhf/platform/base.rb', line 123

def custom_search
  table_value(:custom_search)
end

#data_sourceObject



197
198
199
# File 'lib/bhf/platform/base.rb', line 197

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

#definitionsObject



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

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



205
206
207
# File 'lib/bhf/platform/base.rb', line 205

def entries_per_page
  table_value(:per_page)
end

#fieldsObject



79
80
81
# File 'lib/bhf/platform/base.rb', line 79

def fields
  @fields ||= remove_excludes(default_attrs(form_value(:display), attributes), form_value(:exclude))
end

#formObject



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

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
# 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
    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

#has_file_upload?Boolean

Returns:

  • (Boolean)


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

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



173
174
175
# File 'lib/bhf/platform/base.rb', line 173

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

#hide_deleteObject



181
182
183
# File 'lib/bhf/platform/base.rb', line 181

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

#hide_editObject



169
170
171
# File 'lib/bhf/platform/base.rb', line 169

def hide_edit
  table_value 'hide_edit'
end

#hooks(method) ⇒ Object



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

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

#modelObject



55
56
57
58
59
60
61
# File 'lib/bhf/platform/base.rb', line 55

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

#model_nameObject



63
64
65
# File 'lib/bhf/platform/base.rb', line 63

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

#remove_excludes(attrs, excludes) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/bhf/platform/base.rb', line 71

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

#search?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/bhf/platform/base.rb', line 115

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

#search_field?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/bhf/platform/base.rb', line 119

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

#showObject



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

def show
  @data['show']
end

#show_duplicateObject



177
178
179
# File 'lib/bhf/platform/base.rb', line 177

def show_duplicate
  table_value 'show_duplicate'
end

#show_extra_fieldsObject



201
202
203
# File 'lib/bhf/platform/base.rb', line 201

def show_extra_fields
  show_value(:extra_fields)
end

#sortableObject



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

def sortable
  table_value 'sortable'
end

#sortable_propertyObject



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

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

#tableObject



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

def table
  @data['table']
end

#table_columnsObject



127
128
129
# File 'lib/bhf/platform/base.rb', line 127

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

#table_hide?Boolean

Returns:

  • (Boolean)


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

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

#table_quick_editObject



189
190
191
# File 'lib/bhf/platform/base.rb', line 189

def table_quick_edit
  table_value 'quick_edit'
end

#user_scope?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/bhf/platform/base.rb', line 135

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