Class: Admin

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Admin

Returns a new instance of Admin.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/admin.rb', line 10

def initialize(params)
  @model_blacklist                     = []
  @visible_columns_blacklist           = []
  @editable_columns_blacklist          = []
  @creatable_models_whitelist          = []
  @editable_associations_whitelist     = {}
  @extensions                          = []

  self.model_blacklist                 = params[:model_blacklist]
  self.visible_columns_blacklist       = params[:visible_columns_blacklist]
  self.editable_columns_blacklist      = params[:editable_columns_blacklist]
  self.creatable_models_whitelist      = params[:creatable_models_whitelist]
  self.editable_associations_whitelist = params[:editable_associations_whitelist]
  self.extensions                      = params[:extensions]
end

Instance Attribute Details

#creatable_models_whitelistObject

Returns the value of attribute creatable_models_whitelist.



7
8
9
# File 'app/models/admin.rb', line 7

def creatable_models_whitelist
  @creatable_models_whitelist
end

#editable_associations_whitelistObject

Returns the value of attribute editable_associations_whitelist.



7
8
9
# File 'app/models/admin.rb', line 7

def editable_associations_whitelist
  @editable_associations_whitelist
end

#editable_columns_blacklistObject

Returns the value of attribute editable_columns_blacklist.



7
8
9
# File 'app/models/admin.rb', line 7

def editable_columns_blacklist
  @editable_columns_blacklist
end

#extensionsObject

Returns the value of attribute extensions.



7
8
9
# File 'app/models/admin.rb', line 7

def extensions
  @extensions
end

#model_blacklistObject

Returns the value of attribute model_blacklist.



7
8
9
# File 'app/models/admin.rb', line 7

def model_blacklist
  @model_blacklist
end

#visible_columns_blacklistObject

Returns the value of attribute visible_columns_blacklist.



7
8
9
# File 'app/models/admin.rb', line 7

def visible_columns_blacklist
  @visible_columns_blacklist
end

Instance Method Details

#decorate_column_class(col_name) ⇒ Object



71
72
73
74
75
76
77
78
# File 'app/models/admin.rb', line 71

def decorate_column_class(col_name)
  case col_name
    when 'created_at'
      'format-date'
    when 'updated_at'
      'format-date'
    end
end

#editable_attributes_for_model(my_model) ⇒ Object



85
86
87
88
# File 'app/models/admin.rb', line 85

def editable_attributes_for_model(my_model)
  my_model = my_model.constantize if my_model.is_a? String
  my_model.new.attribute_names - editable_columns_blacklist
end

#get_resource(model_name, id) ⇒ Object



90
91
92
# File 'app/models/admin.rb', line 90

def get_resource(model_name, id)
  model_name.constantize.find(id)
end

#new_resource(model_name) ⇒ Object



94
95
96
# File 'app/models/admin.rb', line 94

def new_resource(model_name)
  model_name.constantize.new
end

#simple_form_field(simple_form_obj, my_model, alternate_input_hash = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/models/admin.rb', line 98

def simple_form_field(simple_form_obj, my_model, alternate_input_hash={})
  editable_attributes = self.editable_attributes_for_model my_model
  collection_validators = my_model.constantize.validators.select{|v| v.options.keys.include? :in }
  html = ""

  # table columns
  editable_attributes.each do |ea|
    if !alternate_input_hash.blank? && ea =~ Regexp.new(alternate_input_hash.keys.first.to_s)
      # non simple_form inputs
      html += "<div class='form-group'>"
      html += simple_form_obj.send(alternate_input_hash.values.first.to_s, ea.to_sym, {}, { class: "form-control" })
      html += "</div>"
    else
      # simple_form inputs
      collection_validator = collection_validators.select{|cv| cv.attributes.include? ea.to_sym}.first
      if collection_validator
        # treat as a collection
        html += simple_form_obj.input(ea.to_sym, collection: collection_validator.send(:delimiter), input_html: {class: 'form-control'}, wrapper_html: {class: 'form-group'})
      else
        if simple_form_obj.object.send(ea.to_sym).is_a? Boolean
          class_for_group = nil
          class_for_control = nil
        else
          class_for_group = "form-group"
          class_for_control = "form-control"
        end
        # is not a collection
        html += simple_form_obj.input(ea.to_sym, input_html: {class: class_for_control}, wrapper_html: {class: class_for_group})
      end
    end
  end

  # model associations
  my_model.constantize.reflections.keys.each do |associated|
    if editable_associations_whitelist[my_model] &&
       editable_associations_whitelist[my_model].include?(associated)
       # TODO: check association type, one to many one to one,
       # and switch appropriate form control
      html += simple_form_obj.association(associated, as: :check_boxes)
    end
  end

  html.html_safe
end

#table_modelsObject



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/admin.rb', line 57

def table_models
  @table_models = []
  ActiveRecord::Base.connection.tables.collect do |table|
    unless model_blacklist && model_blacklist.include?(table.classify)
      begin
        @table_models << table.classify.constantize
      rescue
        nil
      end
    end
  end
  @table_models.compact! || @table_models
end

#visible_attributes_for_model(my_model) ⇒ Object



80
81
82
83
# File 'app/models/admin.rb', line 80

def visible_attributes_for_model(my_model)
  my_model = my_model.constantize if my_model.is_a? String
  my_model.new.attribute_names - visible_columns_blacklist
end