Module: DynamicController::HelperMethods
- Defined in:
- lib/dynamic_controller/helper_methods.rb
Instance Method Summary collapse
- #collection ⇒ Object
- #collection=(value) ⇒ Object
- #handle_error(error) ⇒ Object
- #load_parent_models ⇒ Object
- #model ⇒ Object
- #model=(value) ⇒ Object
- #model_extended_attributes ⇒ Object
- #parent_model ⇒ Object
- #parent_models ⇒ Object
- #resource_class ⇒ Object
- #resource_namespace ⇒ Object
- #search_node_valid?(node, valid_attributes) ⇒ Boolean
- #search_query ⇒ Object
- #search_query_valid? ⇒ Boolean
Instance Method Details
#collection ⇒ Object
16 17 18 |
# File 'lib/dynamic_controller/helper_methods.rb', line 16 def collection instance_variable_get("@#{controller_name}") end |
#collection=(value) ⇒ Object
12 13 14 |
# File 'lib/dynamic_controller/helper_methods.rb', line 12 def collection=(value) instance_variable_set("@#{controller_name}", value) end |
#handle_error(error) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/dynamic_controller/helper_methods.rb', line 51 def handle_error(error) respond_to do |format| format.html { raise error } format.json { render json: error., status: :internal_server_error } end end |
#load_parent_models ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/dynamic_controller/helper_methods.rb', line 28 def load_parent_models parent = nil self.class.parent_resources.each do |resource| if parent parent = parent.send(resource.children_name).find(params[resource.param_name]) else parent = resource.find params[resource.param_name] end instance_variable_set resource.instance_variable_name, parent end end |
#model ⇒ Object
24 25 26 |
# File 'lib/dynamic_controller/helper_methods.rb', line 24 def model instance_variable_get("@#{controller_name.singularize}") end |
#model=(value) ⇒ Object
20 21 22 |
# File 'lib/dynamic_controller/helper_methods.rb', line 20 def model=(value) instance_variable_set("@#{controller_name.singularize}", value) end |
#model_extended_attributes ⇒ Object
58 59 60 |
# File 'lib/dynamic_controller/helper_methods.rb', line 58 def model_extended_attributes resource_class.column_names | resource_class.reflections.map { |k, v| v.klass.column_names.map { |c| "#{k}_#{c}" } }.flatten end |
#parent_model ⇒ Object
47 48 49 |
# File 'lib/dynamic_controller/helper_methods.rb', line 47 def parent_model (parent_models || []).last end |
#parent_models ⇒ Object
40 41 42 43 44 45 |
# File 'lib/dynamic_controller/helper_methods.rb', line 40 def parent_models return nil if self.class.parent_resources.empty? self.class.parent_resources.map do |resource| instance_variable_get resource.instance_variable_name end end |
#resource_class ⇒ Object
4 5 6 |
# File 'lib/dynamic_controller/helper_methods.rb', line 4 def resource_class @resource_class ||= (resource_namespace.to_s.split('::') << controller_name.classify).join('::').constantize end |
#resource_namespace ⇒ Object
8 9 10 |
# File 'lib/dynamic_controller/helper_methods.rb', line 8 def resource_namespace self.class.to_s.deconstantize.constantize end |
#search_node_valid?(node, valid_attributes) ⇒ Boolean
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/dynamic_controller/helper_methods.rb', line 75 def search_node_valid?(node, valid_attributes) return true unless node node.deep_symbolize_keys.each do |k, v| if k == :a return false unless valid_attributes.include?(v['0'.to_sym][:name]) else if v.is_a?(Hash) return false unless search_node_valid?(v, valid_attributes) elsif v.is_a?(Array) v.select { |e| e.is_a?(Hash) }.each { |e| return false unless search_node_valid?(e, valid_attributes) } end end end true end |
#search_query ⇒ Object
62 63 64 |
# File 'lib/dynamic_controller/helper_methods.rb', line 62 def search_query params[:q].is_a?(String) ? NQL.to_ransack(params[:q]) : params[:q] end |
#search_query_valid? ⇒ Boolean
66 67 68 69 70 71 72 73 |
# File 'lib/dynamic_controller/helper_methods.rb', line 66 def search_query_valid? begin search_node_valid? search_query, model_extended_attributes rescue NQL::InvalidExpressionError => ex Rails.logger.debug "Invalid search query: #{params[:q]} | Error: #{ex.}" false end end |