{"id":18546666,"url":"https://github.com/proctoru/multi_model_wizard","last_synced_at":"2025-05-15T07:09:24.403Z","repository":{"id":75928867,"uuid":"599297409","full_name":"ProctorU/multi_model_wizard","owner":"ProctorU","description":"MultiModelWizard is a way to create and update multiple ActiveRecord models using one form object. Creates a smart object for your wizards or forms. Create one form and form object that can update multiple models with ease.","archived":false,"fork":false,"pushed_at":"2023-03-03T21:49:30.000Z","size":25,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-05-15T07:09:05.699Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://rubygems.org/gems/multi_model_wizard","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ProctorU.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-02-08T21:11:48.000Z","updated_at":"2023-02-23T00:24:55.000Z","dependencies_parsed_at":null,"dependency_job_id":"e5f76e5b-4ba0-47a6-a08a-91a6e789a4da","html_url":"https://github.com/ProctorU/multi_model_wizard","commit_stats":{"total_commits":5,"total_committers":2,"mean_commits":2.5,"dds":0.4,"last_synced_commit":"c3160c13de82f19944530b12385ea8552d03ad71"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProctorU%2Fmulti_model_wizard","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProctorU%2Fmulti_model_wizard/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProctorU%2Fmulti_model_wizard/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProctorU%2Fmulti_model_wizard/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ProctorU","download_url":"https://codeload.github.com/ProctorU/multi_model_wizard/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254292042,"owners_count":22046426,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T20:26:27.613Z","updated_at":"2025-05-15T07:09:19.393Z","avatar_url":"https://github.com/ProctorU.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MultiModelWizard\n\nMultiModelWizard is a way to create and update multiple ActiveRecord models using one form object. Creates a smart object for your wizards or forms. Create one form and form object that can update multiple models with ease.\n\n## Install\n\nAdd this to your Gemfile\n```\n  $ gem install multi_model_wizard\n```\n\nThen run `bundle install` and you're ready to start\n\n## Use\n\nInitialize the gem by creating an initializer file and then configuring your settings:\n```\n  # config/initializers/multi_model_wizard.rb\n  #\n  MultiModelWizard.configure do |config|\n    config.store = { location: :redis, redis_instance: Redis.current }\n    config.form_key = 'custom_car_wizard'\n  end\n```\nThe above code snippet is an example configuration. You only need to specify an initializer if you want to change the form key \nor if you want to use Redis as the storage location.\n\nNote: If your form is going to be over 4kb then you will have to use Redis. Data larger than 4kb can not be stored in cookies (which is the default configuration).\n\nCreate a new form object that inherits from the base class. Make sure to override the `form_steps`, `create`, and `update`.\n```\n  # form_objects/custom_vehicle_form.rb\n  #\n  class CustomVehicleForm \u003c FormObject::Base\n    cattr_accessor :form_steps do\n      %i[\n        basic_configuration\n        body\n        engine\n        review\n      ].freeze\n    end\n\n    def create\n      created = false\n      begin\n          ActiveRecord::Base.transaction do\n          car = Car.new(attributes_for(Car))\n          car.parts = car_parts\n          car.save!\n          end\n          created = true\n      rescue StandardError =\u003e err\n          return created       \n      end\n      created\n    end\n\n    def update\n      updated = false\n      begin\n        ActiveRecord::Base.transaction do\n          car = Car.find(car_id)\n          car.attributes = attributes_for(Car)\n          car.parts = car_parts\n          car.save!\n        end\n        updated = true\n      rescue StandardError =\u003e err\n        return updated       \n      end\n      updated\n    end\n  end\n```\n\nUse form in your controller:\n\n```\n  # controllers/vehicle_wizard_controller.rb\n  #\n  def set_vehicle_form\n    @form ||= Wizards::FormObjects::CarForm.create_form do |form|\n                form.add_model Manufacturer\n                form.add_model Dealer\n                form.add_multiple_instance_model model: Part, instances: parts\n                form.add_dynamic_model prefix: 'vehicle', model: Vehicle\n                form.add_extra_attributes prefix: 'vehicle', attributes: %i[leather_origin], model: Vehicle\n              end\n  end\n\n  before_action :set_form_id\n\n  def set_form_id\n    @form_id = params[:vehicle_id]\n  end\n```\n\nSetting form_id will allow the gem to differ between a new wizard from (creating new data) and an existing form (editing existing models)\nNote: The `@form_id` should be set equal to whatever model id you are using in your form route.  \n\nYou can now pass `@form` to your form views and start interacting with user input. The attributes of the form are the model attributes prefixed with the model name. Example:\n```\n  dealer = Dealer.new\n\n  dealer.name\n  # =\u003e nil\n\n  @form.dealer_name\n  # =\u003e nil\n```\n\nIn the above example you might have `dealer_name` as an open text field in your form. That attribute would be mapped to the `Dealer` model and get validated using those validations.\n```\n  # views/vehicle_wizard/basic_configuration.rb\n  #\n  \u003c%= form_for @form, url: my_wizard_path, method: :put do |f| %\u003e\n    \u003c%=  f.text_field :dealer_name  %\u003e\n\n    \u003c%= link_to 'Back', previous_wizard_path, class: 'btn btn-secondary' %\u003e\n    \u003c%= f.submit 'Next', value: 'Next: Configuration', class: 'btn btn-primary'%\u003e\n  \u003c% end %\u003e\n```\n\nModels added with `add_multi_model_instance_model` are different. The form attribute to access these will be the pluralized version of the model name.\n```\n  @form.parts\n  #=\u003e [{ name: nil, type: nil, size: nil}, { name: nil, type: nil, size: nil}]\n```\n\nThese will also be mapped to the model and validated using its validators.\n\n`add_dynamic_model` models initialized with the dynamic attribute method will be referenced using whatever you set as the prefix and then the attribute name.\n```\n  @form.vehicle_type\n  #=\u003e nil\n```\n\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`.\n\n## Contributing\n\n### Content\n\n* Write articles\n* Recording screencasts\n* Submit presentations\n\nPull requests are welcome! Feel free to submit bugs as well.\n\n1. Fork it! ( https://github.com/schneems/wicked/fork )\n2. Create your feature branch: `git checkout -b my-new-feature`\n3. Commit your changes: `git commit -am 'Add some feature'`\n4. Push to the branch: `git push origin my-new-feature`\n5. Create a new Pull Request :D\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fproctoru%2Fmulti_model_wizard","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fproctoru%2Fmulti_model_wizard","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fproctoru%2Fmulti_model_wizard/lists"}