Coder Social home page Coder Social logo

Comments (7)

DmitryTsepelev avatar DmitryTsepelev commented on May 25, 2024

Hi @drewbaumann!

Unfortunately we cannot reach the parent from the class method (because we do not have an access to the instance of Shop::OptinSettings::Details). I decided to try accessing parent from the initializer, but it's empty during the initialization. I ended up with a hacky way:

class OptinSettings
  include StoreModel::Model

  attribute :modal, OptinSettings::Details.to_type, default: OptinSettings::Details.new(:modal) # passing field name explicitly
  attribute :page, OptinSettings::Details.to_type, default: OptinSettings::Details.new(:page) # same
end

class OptinSettings::Details
  include StoreModel::Model

  attribute :body, :string
  attribute :title, :string

  def initialize(parent_attribute)
    super({}) # << otherwise it will try to pass parent_attribute

    self.title = I18n.t("shop.optin_settings.#{parent_attribute}.title")
    self.body = I18n.t("shop.optin_settings.#{parent_attribute}.title")
  end
end

from store_model.

drewbaumann avatar drewbaumann commented on May 25, 2024

@DmitryTsepelev thank you. That makes sense. Does it handle dirty tracking on assignment? Happy to take this into another issue!

from store_model.

DmitryTsepelev avatar DmitryTsepelev commented on May 25, 2024

I guess it should (since it's just a regular assignment), but please don't take my word for it 🙂Also, it feels like it might not work when something is already stored in the database (I'm afraid we should check it in the initializer)

from store_model.

drewbaumann avatar drewbaumann commented on May 25, 2024

Awesome. I appreciate your response!

from store_model.

drewbaumann avatar drewbaumann commented on May 25, 2024

@DmitryTsepelev So interestingly enough this workaround works for records that do not have values under messaging, but when I load a record with an existing value I see:

1]pry(main) > Shop.find(4).optin_settings.modal.body
  Shop Load (1.1ms)  SELECT "shops".* FROM "shops" WHERE "shops"."id" = $1 LIMIT $2  [["id", 4], ["LIMIT", 1]]
=> "translation missing: en.shop.optin_settings.{\"body\"=>\"Make things easy with personalized EVERYTHING. <br/><br/> <b>No commitment. No spamming, ever.</b>\\r\\n\", \"title\"=>\"Never never never never ever run out of products. Ever.\"}.body"

So it is basically injecting the json via string interpolation.

To get around it I had to make the following change:

class Shop::OptinSettings::Details
  include StoreModel::Model

  attribute :body, :string
  attribute :title, :string

  def initialize(parent_attribute)
    super({}) # << otherwise it will try to pass parent_attribute
    if parent_attribute.is_a?(Symbol)
      self.title = I18n.t("shop.optin_settings.#{parent_attribute}.title")
      self.body = I18n.t("shop.optin_settings.#{parent_attribute}.body")
    elsif parent_attribute.is_a?(Hash)
      parent_attribute.each do |key, value|
        assignment_method = "#{key}="
        if respond_to?(assignment_method)
          send(assignment_method, value)
        end
      end
    end
  end
end

from store_model.

DmitryTsepelev avatar DmitryTsepelev commented on May 25, 2024

Sorry I'm a little bit behind things, will try to take a look later this week

from store_model.

DmitryTsepelev avatar DmitryTsepelev commented on May 25, 2024

@drewbaumann I feel like using the initializer was a bad idea, what about this approach?

class OptinSettings
  include StoreModel::Model

  attribute :modal, OptinSettings::Details.to_type, default: OptinSettings::Details.default_for(:modal)
  attribute :page, OptinSettings::Details.to_type, default: OptinSettings::Details.default_for(:page)
end

class OptinSettings::Details
  include StoreModel::Model

  attribute :body, :string
  attribute :title, :string

  def default_for(attribute_name)
    {
      title: I18n.t("shop.optin_settings.#{attribute_name}.title"),
      body: I18n.t("shop.optin_settings.#{attribute_name}.body"
    }
  end
end

from store_model.

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.