Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get all settings in a scope #262

Open
yshmarov opened this issue Jan 19, 2025 · 0 comments
Open

get all settings in a scope #262

yshmarov opened this issue Jan 19, 2025 · 0 comments

Comments

@yshmarov
Copy link

In my settings model I have a scope:

  scope :socials do
    field :instagram, default: "https://www.instagram.com/yaro_the_slav", type: :string
    field :twitter, default: "https://twitter.com/yarotheslav", type: :string
    field :tiktok, default: "", type: :string, help_text: "https://www.tiktok.com/@yaro_the_slav"
    field :linkedin, default: "", type: :string, help_text: "https://www.linkedin.com/in/yarotheslav"
    field :youtube, default: "", type: :string, help_text: "https://www.youtube.com/@yaro_the_slav"
    field :facebook, default: "", type: :string, help_text: "https://www.facebook.com/yarotheslav"
    field :pinterest, default: "", type: :string, help_text: "https://www.pinterest.com/yarotheslav"
    field :twitch, default: "", type: :string, help_text: "https://www.twitch.tv/yarotheslav"
    field :discord, default: "", type: :string, help_text: "https://discord.gg/yarotheslav"
    field :telegram, default: "", type: :string, help_text: "https://t.me/yarotheslav"
    field :whatsapp, default: "", type: :string, help_text: "https://wa.me/yarotheslav"
  end

In a view I want to iterate through all fields in this scope.

I don't see a way to get all fields in a scope.

This works for me:

  def self.get_scope(scope_name)
    defined_fields
      .select { |field| field[:scope] == scope_name }
      .each_with_object({}) { |field, hash| hash[field[:key]] = send(field[:key]) }
  end

Would you consider merging this method?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant