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

Implemented Tramway Form inheritance #44

Merged
merged 7 commits into from
Feb 5, 2024

Conversation

kalashnikovisme
Copy link
Contributor

@kalashnikovisme kalashnikovisme commented Jan 31, 2024

What's changed basically?

Tramway Form inheritance implemented

What's changed for tramway drivers?

Example

Before

class UserForm < TramwayForm
  properties :email, :password
end

class AdminForm < UserForm
  properties :permissions
end

AdminForm.properties # returns [:permissions]

After

class UserForm < TramwayForm
  properties :email, :password
end

class AdminForm < UserForm
  properties :permissions
end

AdminForm.properties # returns [:email, :password, :permissions]

Playlist for the code-review (Spotify)

image

Find more info here

@kalashnikovisme kalashnikovisme changed the title Issue #43. Implemented Tramway Form inheritance Implemented Tramway Form inheritance Jan 31, 2024
README.md Outdated Show resolved Hide resolved
spec/forms/inheritance_form_spec.rb Outdated Show resolved Hide resolved
end
end

def __properties
@properties ||= []
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that you're using @properties ||= [] several times in the base_form.rb class. You could initializes the @properties class instance variable one time when the class is loaded

class << self
   @properties = []

  def property(attribute)
    (__ancestor_properties + @properties).uniq
  end
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've never seen this approach. Are you sure that initializing class-level variables out of methods is nice? 🙂

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's for sure nicer to have one place of default initialization, and I dont see problems with class level variable.

But I also haven't used this approach much, so can't comment if it have no problems at all

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested this approach. For some reason, @properties is not initialized on class load.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see, the variable initialization doesn't pass to child classes. Sorry, then, my bad :)
The initialization still could be isolated with inherited hook, if you willing to go that path, like this

class Parent
  def self.inherited(subclass)
    subclass.instance_variable_set(:@things, [])
  end

  class << self
    def add(value)
      @things << value
    end
  end
end

class ChildDecorator < Parent
  add :third
  add :four
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! 🙂 Thanks!!

btw, Linters are v ahuie 😆

Copy link

@haukot haukot Feb 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, Linters are v ahuie

They are right, it should be @properties, not @@properties :) @@properties will give you effects you don't want, like - if you change child properties, the parent properties also will be changed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I missed something, but looks like we can't use @properties in our case.

subclass.class_variable_set(:@properties, __ancestor_properties)

return NameError: '@properties' is not allowed as a class variable name

Copy link

@haukot haukot Feb 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because it should be instance_variable_set, not class_variable_set. As a class is actually instance of Class itself, and class variables are @@ ones

@kalashnikovisme kalashnikovisme merged commit 906eb67 into main Feb 5, 2024
3 checks passed
@kalashnikovisme kalashnikovisme deleted the 43-implement-inheritance-for-tramway-form branch February 5, 2024 12:10
@@ -1,5 +1,6 @@
# frozen_string_literal: true

# :reek:ClassVariable { enabled: false }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kalashnikovisme this shouldn't be needed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix in the next PR

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.

None yet

2 participants