Giter Club home page Giter Club logo

Comments (8)

lunks avatar lunks commented on August 22, 2024 1

Any updates on this?

from active_attr.

cgriego avatar cgriego commented on August 22, 2024

The functionality for parsing this in ActiveRecord is hiding in Base, kicked off by assign_multiparameter_attributes.

from active_attr.

cgriego avatar cgriego commented on August 22, 2024

This functionality depends on typecasting, or at least knowing the type of the attribute.

from active_attr.

stefanoverna avatar stefanoverna commented on August 22, 2024

Any news on this? Would be awesome :)

from active_attr.

ryanatwork avatar ryanatwork commented on August 22, 2024

Has this been implemented yet?

from active_attr.

quinn avatar quinn commented on August 22, 2024

Are you looking for pull requests?

from active_attr.

boxofrad avatar boxofrad commented on August 22, 2024

FWIW here's an implementation from one of our apps:

# spec/form_objects/form_object_base_spec.rb
require 'rails_helper'

describe FormObjectBase do
  class FooForm < FormObjectBase
    attribute :name
    attribute :cool_date, type: Date
    attribute :something_else, type: Date
  end

  it 'handles multiparameter assignment' do
    form = FooForm.new(
      'cool_date(1i)'      => '1993',
      'cool_date(2i)'      => '2',
      'cool_date(3i)'      => '27',
      'name'               => 'Doofy',
      'something_else(1i)' => '2014',
      'something_else(2i)' => nil,
      'something_else(3i)' => ''
    )

    expect(form.cool_date).to eq Date.new(1993, 2, 27)
    expect(form.name).to eq 'Doofy'
    expect(form.something_else).to be_nil
  end
end
# app/form_objects/form_object_base.rb
class FormObjectBase
  include ActiveAttr::Attributes
  include ActiveAttr::MassAssignment
  include ActiveAttr::BasicModel
  include ActiveAttr::TypecastedAttributes

  def assign_attributes(new_attributes, options = {})
    super(
      expand_multiparameter_attributes(new_attributes),
      options
    )
  end

  private

  def expand_multiparameter_attributes(attributes)
    single_parameter_attributes = {}
    multi_parameter_attributes = {}

    attributes.each do |key, value|
      matches = key.match(/^(?<key>[^\(]+)\((?<index>\d+)i\)$/)

      unless matches
        single_parameter_attributes[key] = value
        next
      end

      args = (multi_parameter_attributes[matches['key']] ||= [])
      args[matches['index'].to_i - 1] = (value.present? ? value.to_i : nil)
    end

    single_parameter_attributes.merge(
      multi_parameter_attributes.inject({}) do |hash, (key, args)|
        if args.all?(&:present?)
          hash.merge(key => _attribute_type(key).new(*args))
        else
          hash
        end
      end
    )
  end
end

from active_attr.

boxofrad avatar boxofrad commented on August 22, 2024

Whoops, left a bug in there (single parameter attributes would be lost) fixed and updated my comment 😄

from active_attr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.