{"id":18336418,"url":"https://github.com/launchpadlab/air_traffic_control","last_synced_at":"2025-04-09T19:55:43.458Z","repository":{"id":59150497,"uuid":"51653101","full_name":"LaunchPadLab/air_traffic_control","owner":"LaunchPadLab","description":null,"archived":false,"fork":false,"pushed_at":"2016-02-13T16:13:19.000Z","size":10,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-09T19:55:40.579Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LaunchPadLab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-02-13T16:07:00.000Z","updated_at":"2016-02-13T16:08:23.000Z","dependencies_parsed_at":"2022-09-13T11:00:32.145Z","dependency_job_id":null,"html_url":"https://github.com/LaunchPadLab/air_traffic_control","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaunchPadLab%2Fair_traffic_control","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaunchPadLab%2Fair_traffic_control/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaunchPadLab%2Fair_traffic_control/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaunchPadLab%2Fair_traffic_control/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LaunchPadLab","download_url":"https://codeload.github.com/LaunchPadLab/air_traffic_control/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248103911,"owners_count":21048245,"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-05T20:07:51.444Z","updated_at":"2025-04-09T19:55:43.425Z","avatar_url":"https://github.com/LaunchPadLab.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"ATC\n===\n\nInstallation\n---\n\nAdd Gem:\n\n```ruby\ngem \"air_traffic_control\"\n```\n\n```\nbundle\n```\n\n\nBasic Usage\n---\n\nCreate a class to process your form and have it inherit from ATC::Form. I typically create the following folder to hold my form classes: app/classes/forms\n\n**app/classes/forms/property.rb**\n\n```ruby\nclass Forms::Property \u003c ATC::Form\n\n  input :bedrooms, :integer\n  input :price, :float\n  input :has_air_conditioning, :boolean\n  input :has_parking, :boolean\n  input :has_laundry, :boolean\n\nend\n```\n\nIn your controller:\n\n```ruby\n  def create\n    @form = Forms::Property.new(params)\n\n    if @form.save\n      redirect_to trips_path\n    else\n      set_index_variables\n      render \"index\"\n    end\n  end\n```\n\nInheritance\n---\n\nGeneral pattern:\n\n- forms/property.rb\n- forms/property/create.rb (inherits from forms/property.rb)\n- forms/property/update.rb (inherits from forms/property.rb)\n\n\n```ruby\n# app/classes/forms/property.rb\nclass Forms::Property \u003c ATC::Form\n\n  input :bedrooms, :integer\n  input :price, :float\n  input :referred_by, :string\n\nend\n\n\n# app/classes/forms/property/create.rb\nclass Forms::Property::Create \u003c Forms::Property\n\n  # override any input, validation, or method here\n\nend\n```\n\nclass Forms::Property::Update \u003c Forms::Property\n\n  # override any input, validation, or method here\n\nend\n```\n\nAssociations\n---\n\nLet's say we are creating a web app where teacher's can create Courses with assignments and resources attached to the courses.\n\nThe modeling would look like:\n\nclass Course \u003c ActiveRecord::Base\n\n  has_many :assignments\n  has_many :resources\n\nend\n\n\n\nWhy a Library for Parsing Forms?\n---\n\nIn my humble opinion, Rails is missing a tool for parsing forms on the backend. Currently the process looks something like this:\n\n```html\n# new.html.erb\n\u003c%= form_for @trip do |trip_builder| %\u003e\n  \u003cdiv class=\"field\"\u003e\n    \u003c%= trip_builder.label :miles %\u003e\n    \u003c%= trip_builder.text_field :miles, placeholder: '50 miles' %\u003e\n  \u003c/div\u003e\n\u003c% end %\u003e\n```\n\n```ruby\n  # trips_controller\n\n  def create\n    @trip = Trip.new(trip_params)\n\n    if @trip.save\n      redirect_to trips_path, notice: \"New trip successfully created.\"\n    else\n      render \"new\"\n    end\n  end\n```\n\nThe problem with this approach is that there is nothing that processes the user inputs before being saved into the database. For example, if the user types \"1,000 miles\" into the form, Rails would store 0 instead of 1000 into our \"miles\" column.\n\nThe \"Services\" concept that many Rails developers favor is on point. Every form should have a corresponding Ruby class whose responsibility is to process the inputs and get the form ready for storage.\n\nHowever, this \"Services\" concept does not have a supporting framework. It is repetitive for every developer, for example, to write code to parse a decimal field that comes into our Ruby controller as a string (as in the example above).\n\nHere is how it may be done right now:\n\n```ruby\nclass Forms::Trip\n\n  attr_accessor :trip\n\n  def initialize(args = {})\n    @trip = Trip.new\n    @trip.miles = parse_miles(args[:miles)\n  end\n\n  def parse_miles(miles_input)\n    regex = /(\\d|[.])/ # only care about numbers and decimals\n    miles_input.scan(regex).join.try(:to_f)\n  end\n\n  def save\n    @trip.save\n  end\n\nend\n```\n\n```ruby\n  # trips_controller.rb\n\n  def create\n    form = Forms::Trip.new(trip_params)\n    @trip = form.trip\n\n    if form.save\n      redirect_to trips_path, notice: \"New trip successfully created.\"\n    else\n      render \"new\"\n    end\n  end\n```\n\nWhile this process is not so bad with only one field and one model, it gets more complex with many different fields, types of inputs, and especially with nested attributes.\n\nA better solution is to have the form service classes inherit from a base \"form parser\" class that can handle the common parsing needs of the community. For example:\n\n```ruby\nclass Forms::Trip \u003c FormParse::Base\n\n  input :miles, :float\n\n  def after_init(args)\n    @trip = Trip.new(to_hash)\n  end\n\nend\n```\n\n```ruby\n  # trips_controller.rb\n  # same as above\n\n  def create\n    form = Forms::Trip.new(trip_params)\n    @trip = form.trip\n\n    if form.save\n      redirect_to trips_path, notice: \"New trip successfully created.\"\n    else\n      render \"new\"\n    end\n  end\n```\n\nThe FormParse::Base class that Forms::Trip inherits from by default performs the proper regex as seen in the original Forms::Trip service object above. We need only define the input key and the type of input and the base class takes care of the heavy lifting.\n\nThe \"to_hash\" method takes the inputs and converts the parsed values into a hash, which produces the following in this case:\n\n```ruby\n  { miles: 1000.0 }\n```\n\nA more complex form would end up with a service class like below:\n\n```ruby\nclass Forms::Trip\n\n  attr_accessor :trip\n\n  def initialize(args = {})\n    @trip = Trip.new\n    @trip.miles = parse_miles(args[:miles)\n    @trip.origin_city = args[:origin_city]\n    @trip.origin_state = args[:origin_state]\n    @trip.departure_datetime = parse_datetime(args[:departure_datetime])\n    @trip.destination_city = args[:destination_city]\n    @trip.destination_state = args[:destination_state]\n    @trip.arrival_datetime = parse_datetime(args[:arrival_datetime])\n  end\n\n  def parse_miles(miles_input)\n    regex = /(\\d|[.])/ # only care about numbers and decimals\n    miles_input.scan(regex).join.try(:to_f)\n  end\n\n  def parse_datetime(datetime_input)\n    Date.strptime(datetime, \"%m/%d/%Y\")\n  end\n\n  def save\n    @trip.save\n  end\n\nend\n```\n\nWith a framework, it would only involve the following:\n\n```ruby\nclass Forms::Trip \u003c FormParse::Base\n\n  input :miles, :float\n  input :origin_city, :string\n  input :origin_state, :string\n  input :departure_datetime, :datetime\n  input :destination_city, :string\n  input :destination_state, :string\n  input :arrival_datetime, :datetime\n\n  def after_init(args)\n    @trip = Trip.new(to_hash)\n  end\n\n  # to_hash produces:\n  # {\n  #   miles: 1000.0,\n  #   origin_city: \"Chicago\",\n  #   origin_state: \"IL\",\n  #   departure_datetime: # DateTime object\n  #   destination_city: \"New York\",\n  #   destination_state: \"NY\",\n  #   arrival_datetime: # DateTime object\n  # }\n\nend\n```\n\nTaking it a step further, if our form inputs are not database backed, we can even validate within our new service object like so:\n\n```ruby\nclass FormParse::Base\n  include ActiveModel::Model\n\n  # ... base parsing code\nend\n\nclass Form::Trip \u003c FormParse::Base\n\n  input :miles, :float\n  input :origin_city, :string\n  input :origin_city, :string\n  input :destination_city, :string\n  input :destination_state, :string\n\n  validates :miles, :origin_city, :origin_state, :destination_city, :destination_state, presence: true\n\n  def after_init(args)\n    @trip = Trip.new(to_hash)\n  end\n\n  def save\n    if valid? \u0026\u0026 trip.valid?\n      trip.save\n    else\n      return false\n    end\n  end\n\nend\n```\n\nOf course, the save method could be abstracted to our base class too, assuming we define which object the Form::Trip class is saving like so:\n\n```ruby\nclass Form::Trip\n\n  def object\n    trip\n  end\n\n  # ... above code here\nend\n\nclass FormParse::Base\n\n  def save\n    if valid? \u0026\u0026 object.valid?\n      object.save\n    else\n      return false\n    end\n  end\nend\n\n```\n\nBut where we really can see benefits from a \"framework\" for parsing our Rails forms is when we start to use nested attributes. For example, if we abstract the origin and destination fields to a \"Location\" model, we could build out a separate service class to handle parsing our \"Location\" form (even if it is in the context of a parent object like Trip).\n\n```ruby\nclass Forms::Location \u003c FormParse::Base\n\n  input :city, :string\n  input :state, :string\n  input :departure_datetime, :datetime\n  input :arrival_datetime, :datetime\n\n  validates :city, :state :presence =\u003e true\n\nend\n\nclass Forms::Trip \u003c FormParse::Base\n\n  input :miles, :float\n  input :origin_attributes, :belongs_to, class: 'location'\n  input :destination_attributes, :belongs_to, class: 'location'\n\n  validates :miles, presence: true\n\n  def after_init(args)\n    @trip = Trip.new(to_hash)\n  end\nend\n```\n\nThe FormParse::Base class will recoginze the :belongs_to relationship and utilize the Location service class to parse the part of the params hash that correspond with our destination and origin models. This is great because if there is anywhere that the user can update just the origin or destination of the trip, we could just reuse that Location service object to parse the form.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaunchpadlab%2Fair_traffic_control","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flaunchpadlab%2Fair_traffic_control","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaunchpadlab%2Fair_traffic_control/lists"}