Module: PowerApi::GeneratorHelper::ActiveRecordResource
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/power_api/generator_helper/active_record_resource.rb
Overview
rubocop:disable Metrics/ModuleLength
Instance Method Summary collapse
- #attributes_names ⇒ Object
- #attributes_symbols_text_list ⇒ Object
- #camel ⇒ Object
- #camel_plural ⇒ Object
- #class_definition_line ⇒ Object
- #id ⇒ Object
- #optional_resource_attributes ⇒ Object
- #path ⇒ Object
- #permitted_attributes ⇒ Object
- #permitted_attributes_names ⇒ Object
- #permitted_attributes_symbols_text_list ⇒ Object
- #plural ⇒ Object
- #plural_titleized ⇒ Object
- #required_attributes_names ⇒ Object
- #required_resource_attributes(include_id: false) ⇒ Object
- #resource_attributes=(collection) ⇒ Object
- #resource_name=(value) ⇒ Object
- #snake_case ⇒ Object
- #titleized ⇒ Object
- #upcase ⇒ Object
- #upcase_plural ⇒ Object
Instance Method Details
#attributes_names ⇒ Object
77 78 79 |
# File 'lib/power_api/generator_helper/active_record_resource.rb', line 77 def attributes_names extract_attrs_names(resource_attributes) end |
#attributes_symbols_text_list ⇒ Object
105 106 107 |
# File 'lib/power_api/generator_helper/active_record_resource.rb', line 105 def attributes_symbols_text_list attrs_to_symobls_text_list(attributes_names) end |
#camel ⇒ Object
45 46 47 |
# File 'lib/power_api/generator_helper/active_record_resource.rb', line 45 def camel resource_name.camelize end |
#camel_plural ⇒ Object
49 50 51 |
# File 'lib/power_api/generator_helper/active_record_resource.rb', line 49 def camel_plural camel.pluralize end |
#class_definition_line ⇒ Object
73 74 75 |
# File 'lib/power_api/generator_helper/active_record_resource.rb', line 73 def class_definition_line "class #{camel} < ApplicationRecord\n" end |
#id ⇒ Object
33 34 35 |
# File 'lib/power_api/generator_helper/active_record_resource.rb', line 33 def id "#{snake_case}_id" end |
#optional_resource_attributes ⇒ Object
101 102 103 |
# File 'lib/power_api/generator_helper/active_record_resource.rb', line 101 def optional_resource_attributes permitted_attributes.reject { |attr| attr[:required] } end |
#path ⇒ Object
69 70 71 |
# File 'lib/power_api/generator_helper/active_record_resource.rb', line 69 def path "app/models/#{snake_case}.rb" end |
#permitted_attributes ⇒ Object
89 90 91 92 93 |
# File 'lib/power_api/generator_helper/active_record_resource.rb', line 89 def permitted_attributes resource_attributes.reject do |attr| [:created_at, :updated_at, :id].include?(attr[:name]) end end |
#permitted_attributes_names ⇒ Object
85 86 87 |
# File 'lib/power_api/generator_helper/active_record_resource.rb', line 85 def permitted_attributes_names extract_attrs_names(permitted_attributes) end |
#permitted_attributes_symbols_text_list ⇒ Object
109 110 111 |
# File 'lib/power_api/generator_helper/active_record_resource.rb', line 109 def permitted_attributes_symbols_text_list attrs_to_symobls_text_list(permitted_attributes_names) end |
#plural ⇒ Object
53 54 55 |
# File 'lib/power_api/generator_helper/active_record_resource.rb', line 53 def plural snake_case.pluralize end |
#plural_titleized ⇒ Object
65 66 67 |
# File 'lib/power_api/generator_helper/active_record_resource.rb', line 65 def plural_titleized plural.titleize end |
#required_attributes_names ⇒ Object
81 82 83 |
# File 'lib/power_api/generator_helper/active_record_resource.rb', line 81 def required_attributes_names extract_attrs_names(required_resource_attributes) end |
#required_resource_attributes(include_id: false) ⇒ Object
95 96 97 98 99 |
# File 'lib/power_api/generator_helper/active_record_resource.rb', line 95 def required_resource_attributes(include_id: false) resource_attributes.select do |attr| attr[:required] || (include_id && attr[:name] == :id) end end |
#resource_attributes=(collection) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/power_api/generator_helper/active_record_resource.rb', line 23 def resource_attributes=(collection) attributes = format_attributes(collection) if attributes.count == 1 && attributes.first[:name] == :id raise PowerApi::GeneratorError.new("at least one attribute must be added") end @resource_attributes = attributes end |
#resource_name=(value) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/power_api/generator_helper/active_record_resource.rb', line 9 def resource_name=(value) @resource_name = value if !resource_class raise PowerApi::GeneratorError.new( "Invalid resource name. Must be the snake_case representation of a ruby class" ) end if !resource_is_active_record_model? raise PowerApi::GeneratorError.new("resource is not an active record model") end end |
#snake_case ⇒ Object
57 58 59 |
# File 'lib/power_api/generator_helper/active_record_resource.rb', line 57 def snake_case resource_name.underscore end |
#titleized ⇒ Object
61 62 63 |
# File 'lib/power_api/generator_helper/active_record_resource.rb', line 61 def titleized resource_name.titleize end |
#upcase ⇒ Object
37 38 39 |
# File 'lib/power_api/generator_helper/active_record_resource.rb', line 37 def upcase snake_case.upcase end |
#upcase_plural ⇒ Object
41 42 43 |
# File 'lib/power_api/generator_helper/active_record_resource.rb', line 41 def upcase_plural plural.upcase end |