Comments (7)
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.
@DmitryTsepelev thank you. That makes sense. Does it handle dirty tracking on assignment? Happy to take this into another issue!
from store_model.
I guess it should (since it's just a regular assignment), but please don't take my word for it
from store_model.
Awesome. I appreciate your response!
from store_model.
@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.
Sorry I'm a little bit behind things, will try to take a look later this week
from store_model.
@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)
- Presence validation fails if value is false HOT 2
- How to attach active storage files? HOT 3
- Array of array support HOT 3
- One of depending on another model's column HOT 4
- Validate any Hash HOT 1
- Encrypt attributes HOT 2
- Aliasing an attribute HOT 1
- GraphQL input type fails to cast HOT 4
- ArgumentError: wrong number of arguments (given 1, expected 0) in random model HOT 3
- Assignment Doesn't work in OneOf case HOT 3
- Delegation of `fetch` to `attributes` causes issues HOT 2
- NameError: uninitialized constant StoreModel::Types::ArrayType HOT 3
- Default values for attributes when retrieving the store model HOT 1
- Decoding Custom Types HOT 1
- Allow an option to disallow "UnknownAttributes" behavior HOT 1
- ActiveRecord validation contexts don't propagate HOT 1
- Override methods HOT 3
- Broken defaults in 2.0 HOT 2
- Attribute encryption with ActiveRecord::Encryption HOT 1
- StoreModel Stringifying json when saving. HOT 14
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from store_model.