Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to pass reflex action names to reflex generator #224

Merged
merged 4 commits into from
Jun 27, 2020

Conversation

marcoroth
Copy link
Member

@marcoroth marcoroth commented Jun 1, 2020

Type of PR (feature, enhancement, bug fix, etc.)

Enhancement

Description

This PR allows to call the stimulus_reflex generator with reflex action names.

e.g. rails g stimulus_reflex User update do_stuff

which will generate:

# frozen_string_literal: true

class UserReflex < ApplicationReflex
  [...]

  def update
  end
  
  def do_stuff
  end
end

Lifecycle methods as comments in the generated Stimulus controller

I also added the feature, that the generator adds all lifecycle methods for each given action as a comment in the generated stimulus controller. I don't know if this adds a lot of value or if we should remove it again because it adds a lot of unnecessary "noise".

It would look like this:

import ApplicationController from './application_controller'

export default class extends ApplicationController {
  [...]

  // beforeUpdate(element, reflex) {
  //  console.log("before update", element, reflex)
  // }

  // updateSuccess(element, reflex) {
  //   console.log("update success", element, reflex)
  // }

  // updateError(element, reflex, error) {
  //   console.error("update error", element, reflex, error)
  // }

  // afterUpdate(element, reflex, error) {
  //   console.log("after update", element, reflex, error)
  // }

  // beforeDoStuff(element, reflex) {
  //  console.log("before do_stuff", element, reflex)
  // }

  // doStuffSuccess(element, reflex) {
  //   console.log("do_stuff success", element, reflex)
  // }

  // doStuffError(element, reflex, error) {
  //   console.error("do_stuff error", element, reflex, error)
  // }

  // afterDoStuff(element, reflex, error) {
  //   console.log("after do_stuff", element, reflex, error)
  // }
}

Refactoring

I also took the opportunity to clean up the StimulusReflexGenerator class a little bit. Let me know if this isn't a change you'd like to see.

I did both, the lifecycle method generation and the refactoring, in two separate commits to easily revert them if needed.

Fixes #219 (/cc @julianrubisch).

Why should this be added

This change lets the user even go faster if the user already knows how the reflex should be structured.

Checklist

  • My code follows the style guidelines of this project
  • Checks (StandardRB & Prettier-Standard) are passing

@hopsoft hopsoft merged commit c8417c7 into stimulusreflex:master Jun 27, 2020
jasoncharnes pushed a commit to jasoncharnes/stimulus_reflex that referenced this pull request Jul 1, 2020
…#224)

* allow to pass reflex action names to reflex generator

Resovles stimulusreflex#219

This commit allows to call the stimulus_reflex generator with actions names

e.g. rails g stimulus_reflex User update do_stuff

* also generate all lifecycle methods as comments in stimulus controller

* refactor and simplify StimulusReflexGenerator and templates

* let standardrb do its job 😉
@marcoroth marcoroth deleted the generator-reflex-actions branch November 9, 2020 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tweak the generator so we can specify reflex actions
2 participants