DmitryTsepelev / store_model

Work with JSON-backed attributes as ActiveRecord-ish models

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] How would you use this with rails_admin?

drewbaumann opened this issue · comments

I was wondering if any of you have used rails_admin to edit fields accessible via store_model. If so I am looking for best practices!

Thank you.

Well I figured out a solution that works, and I am happy to hear about methods that have worked well for others.

Here is my working example:

app/models/shop/messaging.rb

class Shop::Messaging
  include StoreModel::Model

  attribute :reminder, :string, default: I18n.t("sms.reminder_notice")
  attribute :replenish, :string, default: I18n.t("sms.replenish_notice")
  attribute :welcome, :string, default: I18n.t("sms.welcome_no_name")
end

app/models/shop.rb

class Shop < ActiveRecord
  ...
  attribute :messaging, Shop::Messaging.to_type
  ...
end

config/initializers/rails_admin.rb

...
config.model Shop do
  edit do
    group "Messaging" do
      label "Messaging"
      help "Please fill all information related to notifications sent to users"
      field :messaging do
        label false
        help false
        partial "shops/messaging_partial"
      end
    end
  end
end

app/views/rails_admin/main/shops/_messaging_partial.html.erb

<%= form.fields_for :messaging do |messaging_attributes|%>
  <div class="form-group">
    <label>Welcome Copy</label>
    <%= messaging_attributes.text_area :welcome, value: form.object.messaging.welcome, class: "form-control" %>
  </div>
  <div class="form-group">
    <label>Replenish Copy</label>
    <%= messaging_attributes.text_area :replenish, value: form.object.messaging.replenish, class: "form-control" %>
  </div>
  <div class="form-group">
    <label>Reminder Copy</label>
    <%= messaging_attributes.text_area :reminder, value: form.object.messaging.reminder, class: "form-control" %>
  </div>
<% end %>

The result:

Image 2020-10-28 at 4 14 28 PM

Hi @drewbaumann! I didn't try to use the gem along with rails_admin, so I don't have much to share 🙂Do you want to take a lead on adding your findings to the documentation?

@DmitryTsepelev Sure I can do that, but it will have to wait till I wrap up implementing this feature!