Module: SuperAccessors::Store
- Defined in:
- lib/super_accessors/store.rb
Instance Method Summary collapse
-
#any_to_b(any) ⇒ Object
for boolean convert.
-
#any_to_i(any) ⇒ Object
converter.
-
#any_to_s(any) ⇒ Object
for string converter.
-
#store_checkboxes_setup(store_name, attrs) ⇒ Object
get the checkboxes true item list & i18n translation.
-
#store_define(store_name, option) ⇒ Object
This function will instead of store function, now support string, boolean, integer.
Instance Method Details
#any_to_b(any) ⇒ Object
for boolean convert
12 13 14 15 16 |
# File 'lib/super_accessors/store.rb', line 12 def any_to_b(any) str = any.to_s return false if str.blank? || str =~ (/(false|no|0)$/i) return true end |
#any_to_i(any) ⇒ Object
converter
4 5 6 |
# File 'lib/super_accessors/store.rb', line 4 def any_to_i(any) any.to_i end |
#any_to_s(any) ⇒ Object
for string converter
8 9 10 |
# File 'lib/super_accessors/store.rb', line 8 def any_to_s(any) any.to_s end |
#store_checkboxes_setup(store_name, attrs) ⇒ Object
get the checkboxes true item list & i18n translation
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/super_accessors/store.rb', line 46 def store_checkboxes_setup(store_name, attrs) define_method("#{store_name}_keys") do list = []; attrs.each do |attr| if send(attr) == true list.push(attr) end end list end define_method("#{store_name}_options") do @i18n_base_scope = [:activerecord, :options, self.class.name.underscore, store_name] list = []; attrs.each do |attr| if send(attr) == true list.push(I18n.t("#{attr}", scope: @i18n_base_scope, :default => attr.to_s.humanize)) end end list end end |
#store_define(store_name, option) ⇒ Object
This function will instead of store function, now support string, boolean, integer
Example 1: store_define :contact_info, :s, tel: :s, mobile: :s, available: :b
Example 2 (checkboxes) store_define :role, accessors: :b, manager: :b, user: :b, checkboxes: true
you have to setup boolean type of all attribute if you are using checkboxes it provide the function
1. show all option of true item
2. show all option of true item with i18n translation
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/super_accessors/store.rb', line 30 def store_define(store_name, option) column_with_datatype = option[:accessors] option[:accessors] = option[:accessors].keys # setting the activerecord store store store_name, option # define the virtual attrs column_with_datatype.each do |attr, type| send("store_key_type_#{type.to_s}", store_name, attr) end # setting the checkboxs if option[:checkboxes] store_checkboxes_setup store_name, option[:accessors] end end |