### Install Store Attribute Gem Source: https://github.com/palkan/store_attribute/blob/master/README.md Add the appropriate gem version to your Gemfile based on your Rails version. ```ruby gem "store_attribute", "~> 1.0" ``` ```ruby gem "store_attribute", "~> 0.8.0" ``` ```ruby gem "store_attribute", "~> 0.4.0" ``` -------------------------------- ### Default Value Fallback Behavior Source: https://github.com/palkan/store_attribute/blob/master/README.md By default, `store_attribute` provides default values even for persisted records where the attribute is not present in the store. This example shows the default behavior and how to disable it. ```ruby user = User.create!(extra: {}) user.expired_at #=> 2022-03-19 ``` ```ruby class User < ApplicationRecord self.store_attribute_unset_values_fallback_to_default = false end user = User.create!(extra: {}) user.expired_at #=> nil ``` -------------------------------- ### Interact with Store Attributes Source: https://github.com/palkan/store_attribute/blob/master/README.md Demonstrates creating a model instance, setting attributes, checking types, and observing default value behavior. Note that explicit store updates bypass type casting. ```ruby u = MegaUser.new(active: false, login_at: "2015-01-01 00:01", ratio: "63.4608") u.login_at.is_a?(DateTime) # => true u.login_at = DateTime.new(2015, 1, 1, 11, 0, 0) u.ratio # => 63 u.active # => false # Default value is set u.color # => red # Default array is set u.colors # => ["red", "blue"] # A dynamic default can also be provided u.data # => Current time # And we also have a predicate method u.active? # => false u.reload # After loading record from db store contains casted data u.settings["login_at"] == DateTime.new(2015, 1, 1, 11, 0, 0) # => true # If you update store explicitly then the value returned # by accessor isn't type casted u.settings["ratio"] = "3.141592653" u.ratio # => "3.141592653" # On the other hand, writing through accessor set correct data within store u.ratio = "3.141592653" u.ratio # => 3 u.settings["ratio"] # => 3 ``` -------------------------------- ### Set Default Values for Store Attributes Source: https://github.com/palkan/store_attribute/blob/master/README.md Demonstrates how to set default values for store attributes using a lambda or a static value. Defaults are applied on new record creation if the store attribute is not explicitly set. ```ruby # Database schema create_table("users") do |t| t.string :name t.jsonb :extra end class RawUser < ActiveRecord::Base self.table_name = "users" end class User < ActiveRecord::Base attribute :name, :string, default: "Joe" store_attribute :extra, :expired_at, :date, default: -> { 2.days.from_now } end Date.current #=> 2022-03-17 user = User.new user.name #=> "Joe" user.expired_at #=> 2022-03-19 user.save! raw_user = RawUser.find(user.id) raw_user.name #=> "Joe" raw_user.expired_at #=> 2022-03-19 another_raw_user = RawUser.create! another_user = User.find(another_raw_user.id) another_user.name #=> nil another_user.expired_at #=> nil ``` -------------------------------- ### Define Store Attributes with `store` Source: https://github.com/palkan/store_attribute/blob/master/README.md Define store attributes and their types directly within the `store` declaration, providing a concise way to configure multiple attributes. ```ruby class User < ActiveRecord::Base store :settings, accessors: [:color, :homepage, login_at: :datetime], coder: JSON end ``` -------------------------------- ### Global Default for Fallback Behavior Source: https://github.com/palkan/store_attribute/blob/master/README.md Configure the global default for `store_attribute_unset_values_fallback_to_default` in an initializer file to control the fallback behavior across your application. ```ruby # config/initializers/store_attribute.rb # # or ``` -------------------------------- ### Define Store Accessors with `store_accessor` Source: https://github.com/palkan/store_attribute/blob/master/README.md An alternative method to define store accessors with type casting using the `store_accessor` method, similar to Rails' built-in functionality. ```ruby class SuperUser < User store_accessor :settings, :privileges, login_at: :datetime end ``` -------------------------------- ### Configure Unset Values Fallback Source: https://github.com/palkan/store_attribute/blob/master/README.md Set `store_attribute_unset_values_fallback_to_default` to `false` in `config/application.rb` to disable falling back to default values when an attribute is unset. This configuration should be placed at the application level. ```ruby StoreAttribute.store_attribute_unset_values_fallback_to_default = false ``` -------------------------------- ### Define Store Attributes with Type Casting Source: https://github.com/palkan/store_attribute/blob/master/README.md Use `store_attribute` to define accessors with specific types and options for a given store. Type casting occurs on write and load, but not on explicit store updates. ```ruby class MegaUser < User store_attribute :settings, :ratio, :integer, limit: 1 store_attribute :settings, :login_at, :datetime store_attribute :settings, :active, :boolean store_attribute :settings, :color, :string, default: "red" store_attribute :settings, :colors, :json, default: ["red", "blue"] store_attribute :settings, :data, :datetime, default: -> { Time.now } end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.