Class: Kablam::KablamRecord
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Kablam::KablamRecord
- Defined in:
- lib/kablam/kablam_record.rb
Class Method Summary collapse
- .choices(field, locale) ⇒ Object
- .field_set ⇒ Object
- .fields ⇒ Object
- .form_choices ⇒ Object
- .prep_form_field ⇒ Object
- .set_fields ⇒ Object
- .slack_hook ⇒ Object
Instance Method Summary collapse
- #html_id ⇒ Object
- #identifier ⇒ Object
- #input(field) ⇒ Object
- #kablam_options ⇒ Object
- #opt_for(field) ⇒ Object
- #slack_message ⇒ Object
- #standard_hash ⇒ Object
- #undoable? ⇒ Boolean
Class Method Details
.choices(field, locale) ⇒ Object
130 131 132 133 134 |
# File 'lib/kablam/kablam_record.rb', line 130 def self.choices(field, locale) c_set = self.form_choices[field] return false if c_set.blank? c_set.map{|choice| {value: choice[:value], label: (choice[:label][locale.to_s] || "CHOICE TRANSLATION NOT FOUND! PLEASE CONTACT STAFF!")}} end |
.field_set ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/kablam/kablam_record.rb', line 93 def self.field_set field_types = { input: [], text: [], hidden: [], select: [], datetime: [], checkbox_array: [], checkbox_boolean: [], multi_inputs: [], file_upload: [], exclude: ["id", "created_at", "updated_at", "destroyed_at"] } user_fields = set_fields user_fields[:exclude] += field_types[:exclude] if user_fields[:exclude].present? field_types.merge user_fields.select { |k| field_types.keys.include? k } end |
.fields ⇒ Object
89 90 91 |
# File 'lib/kablam/kablam_record.rb', line 89 def self.fields result = self.attribute_names - ["id", "created_at", "updated_at", "destroyed_at"] end |
.form_choices ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/kablam/kablam_record.rb', line 69 def self.form_choices puts "For fields w/ choices (select, checkboxes, etc.)" puts "You can setup the values/choices by creating a method:" puts "def self.form_choices" puts "example:" puts " { \"field_name\" => [{value: \"value\", label: { \"locale\"=> \"locale_label\" } }] }" {} end |
.prep_form_field ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/kablam/kablam_record.rb', line 112 def self.prep_form_field # This method will prepare hash of hashes # for each data-field w/ the form input type. # ex: date, radio, select, etc. obj = self.new hashy = {} fields_array = self.fields fields_array.each do |k| hashy[k] = :not_set hashy[k] = obj.column_for_attribute(k).type if obj.column_for_attribute(k).type hashy[k] = :input if [:string, :float, :integer].include? hashy[k] hashy[k] = :checkbox_boolean if [:boolean].include? hashy[k] self.field_set.keys.each{|type| hashy[k] = type if self.field_set[type].include?(k)} end hashy end |
.set_fields ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/kablam/kablam_record.rb', line 52 def self.set_fields puts "You haven't setup Kablam forms for #{self.to_s} yet!" puts "create a method in your model called:" puts "" puts "def self.set_fields" puts "# Then setup the roles of each column name" puts "All the types: checkbox_array, checkbox_boolean, file_upload, hidden, input, multi_inputs, select, text, exclude" puts "Example:" puts "{" puts " select: [\"role\", \"column_2\"]," puts " file_upload: ['column_3']," puts " exclude: [\"id\", \"created_at\", \"user_id\", \"updated_at\"]" puts "}" {} end |
.slack_hook ⇒ Object
35 36 37 |
# File 'lib/kablam/kablam_record.rb', line 35 def self.slack_hook nil end |
Instance Method Details
#html_id ⇒ Object
5 6 7 |
# File 'lib/kablam/kablam_record.rb', line 5 def html_id "#{model_name.singular}-#{id}" end |
#identifier ⇒ Object
85 86 87 |
# File 'lib/kablam/kablam_record.rb', line 85 def identifier send(self.class.fields.first) end |
#input(field) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/kablam/kablam_record.rb', line 18 def input(field) form_name = self.class.to_s.underscore kablam_scope = "kablam_forms.#{self.class.table_name}.#{field}" { name: "#{form_name}[#{field}]", field: field.to_sym, value: send(field), label: I18n.translate(:label, scope: kablam_scope, default: ""), pretext: I18n.translate(:pretext, scope: kablam_scope, default: ""), placeholder: I18n.translate(:placeholder, scope: kablam_scope, default: ""), hint: I18n.translate(:hint, scope: kablam_scope, default: ""), choices: self.class.choices(field, I18n.locale), onchange: I18n.translate(:onchange, scope: kablam_scope, default: ""), true_statement: I18n.translate(:true_statement, scope: kablam_scope, default: "") } end |
#kablam_options ⇒ Object
43 44 45 |
# File 'lib/kablam/kablam_record.rb', line 43 def {} end |
#opt_for(field) ⇒ Object
39 40 41 |
# File 'lib/kablam/kablam_record.rb', line 39 def opt_for(field) Hash[self.class.attribute_names.map{|x| [x,{}]}].merge(self.)[field] end |
#slack_message ⇒ Object
47 48 49 50 |
# File 'lib/kablam/kablam_record.rb', line 47 def puts "You have not setup slack message for this model.\nto prepare a slack_message, please add method 'slack_message'\nwith content of a hash with keys [:create, :update, :destroy].\n\nFor each key, prepare a value hash like this:\n ...\n create: {\n pretext: 'some text',\n author: 'some name',\n title: 'some title',\n text: ''\n },\n ...\n\nHope this helps!!" {create: {}, update: {}, destroy: {}} end |
#standard_hash ⇒ Object
13 14 15 16 |
# File 'lib/kablam/kablam_record.rb', line 13 def standard_hash serializable_hash.except("created_at", "updated_at").reject{|k,v|v.blank?} end |
#undoable? ⇒ Boolean
9 10 11 |
# File 'lib/kablam/kablam_record.rb', line 9 def undoable? attributes.include?("destroyed_at") end |