Sha256: ab25d8413b2e62ba2065ba28f63a9b3931dd2a4031a421eafa1f6c7a989e07a4

Contents?: true

Size: 1.55 KB

Versions: 2

Compression:

Stored size: 1.55 KB

Contents

module WebammToRails
  module Sources
    module Controllers
      module Actions
        module UpdateDefinition
          class Presenter
            def initialize(table_name:, crud_definition:)
              @table_name = table_name
              @crud_definition = crud_definition
            end

            def render
              template_path = File.expand_path('template.erb', __dir__)
              template_content = File.read(template_path)
              raw_content = ERB.new(template_content, trim_mode: '-').result(instance_eval { binding })

              ::WebammToRails::Utils::FormatCode.call(raw_content)
            end

            def access_scope = :public

            def render?
              @crud_definition.actions.find { |action| action.name == 'update' }.present?
            end

            private

            def variable_name
              "@#{@table_name.singularize}"
            end

            def find_call
              "#{@table_name.classify}.find(params[:id])"
            end

            def update_call
              "#{variable_name}.update(#{@table_name.singularize}_params)"
            end

            def redirection_path
              if @crud_definition.actions.find { |action| action.name == 'index' }
                "#{@table_name}_path"
              elsif @crud_definition.actions.find { |action| action.name == 'show' }
                "#{@table_name.singularize}_path(#{variable_name})"
              else
                "root_path"
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
webamm_to_rails-7.0.1 lib/webamm_to_rails/sources/controllers/actions/update_definition/presenter.rb
webamm_to_rails-7.0.0 lib/webamm_to_rails/sources/controllers/actions/update_definition/presenter.rb