Class: AdminData::Util
- Inherits:
-
Object
- Object
- AdminData::Util
- Defined in:
- lib/admin_data/util.rb
Class Method Summary collapse
- .associations_for(klass, association_type) ⇒ Object
- .build_sort_options(klass, sortby) ⇒ Object
- .camelize_constantize(klassu) ⇒ Object
-
.constantize_klass(klass_name) ⇒ Object
klass_name = model_name.sub(/.rb$/,”).camelize constantize_klass(klass_name).
- .custom_value_for_column(column, model) ⇒ Object
- .exception_info(e) ⇒ Object
- .get_serialized_value(html, column_value) ⇒ Object
- .label_values_pair_for(model, view) ⇒ Object
-
.or_merge_conditions(klass, *conditions) ⇒ Object
Rails method merge_conditions ANDs all the conditions.
- .pluralize(count, text) ⇒ Object
- .string_representation_of_data(value) ⇒ Object
-
.write_to_file(data, mode, *path) ⇒ Object
Usage: write ‘hello world’ to tmp/hello.txt file Util.write_to_file(‘hello world’, ‘a+’, ‘tmp’, ‘hello.txt’).
Class Method Details
.associations_for(klass, association_type) ⇒ Object
91 92 93 94 95 |
# File 'lib/admin_data/util.rb', line 91 def self.associations_for(klass, association_type) klass.name.camelize.constantize.reflections.values.select do |value| value.macro == association_type end end |
.build_sort_options(klass, sortby) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/admin_data/util.rb', line 79 def self.(klass, sortby) klass.columns.inject('') do |result, column| name = column.name selected_text = (sortby == "#{name} desc") ? "selected='selected'" : '' result << "<option value='#{name} desc' #{selected_text}>#{name} desc</option>" selected_text = (sortby == "#{name} asc") ? "selected='selected'" : '' result << "<option value='#{name} asc' #{selected_text}>#{name} asc</option>" end.html_safe end |
.camelize_constantize(klassu) ⇒ Object
47 48 49 50 |
# File 'lib/admin_data/util.rb', line 47 def self.camelize_constantize(klassu) klasss = klassu.camelize self.constantize_klass(klasss) end |
.constantize_klass(klass_name) ⇒ Object
klass_name = model_name.sub(/.rb$/,”).camelize constantize_klass(klass_name)
54 55 56 57 58 |
# File 'lib/admin_data/util.rb', line 54 def self.constantize_klass(klass_name) klass_name.split('::').inject(Object) do |klass, part| klass.const_get(part) end end |
.custom_value_for_column(column, model) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/admin_data/util.rb', line 12 def self.custom_value_for_column(column, model) # some would say that if I use try method then I will not be raising exception and # I agree. However in this case for clarity I would prefer to not to have try after each call begin column_name = column.respond_to?(:name) ? column.name : column tmp = AdminData.config.column_settings[model.class.name.to_s] _proc = tmp.fetch(column_name.intern) _proc.call(model) rescue model.send(column_name) end end |
.exception_info(e) ⇒ Object
97 98 99 |
# File 'lib/admin_data/util.rb', line 97 def self.exception_info(e) "#{e.class}: #{e.}#$/#{e.backtrace.join($/)}" end |
.get_serialized_value(html, column_value) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/admin_data/util.rb', line 25 def self.get_serialized_value(html, column_value) html << %{ <i>Cannot edit serialized field.</i> } unless column_value.blank? html << %{ <i>Raw contents:</i><br/> } html << column_value.inspect end html.join end |
.label_values_pair_for(model, view) ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/admin_data/util.rb', line 4 def self.label_values_pair_for(model, view) data = model.class.columns.inject([]) do |sum, column| tmp = view.get_value_for_column(column, model, :limit => nil) sum << [ column.name, (tmp.html_safe? ? tmp : view.send(:h,tmp)) ] end data end |
.or_merge_conditions(klass, *conditions) ⇒ Object
Rails method merge_conditions ANDs all the conditions. I need to ORs all the conditions
39 40 41 42 43 44 45 |
# File 'lib/admin_data/util.rb', line 39 def self.or_merge_conditions(klass, *conditions) s = ') OR (' cond = conditions.inject([]) do |sum, condition| condition.blank? ? sum : sum << klass.send(:sanitize_sql, condition) end.compact.join(s) "(#{cond})" unless cond.blank? end |
.pluralize(count, text) ⇒ Object
34 35 36 |
# File 'lib/admin_data/util.rb', line 34 def self.pluralize(count, text) count > 1 ? text+'s' : text end |
.string_representation_of_data(value) ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/admin_data/util.rb', line 68 def self.string_representation_of_data(value) case value when BigDecimal value.to_s when Date, DateTime, Time "'#{value.to_s(:db)}'" else value.inspect end end |
.write_to_file(data, mode, *path) ⇒ Object
Usage: write ‘hello world’ to tmp/hello.txt file Util.write_to_file(‘hello world’, ‘a+’, ‘tmp’, ‘hello.txt’)
62 63 64 65 66 |
# File 'lib/admin_data/util.rb', line 62 def self.write_to_file(data, mode, *path) path = File.(Rails.root.join(*path.flatten.compact)) FileUtils.mkdir_p(File.dirname(path)) File.open(path, mode) {|fh| fh.puts(data) } end |