# app/models/user.rb
class User < ApplicationRecord
FEATURES = %i[enable_post_meta_description].freeze
store :features, accessors: User::FEATURES
end
# app/policies/feature/enable_post_meta_description_policy.rb
class Feature::EnablePostMetaDescriptionPolicy < ApplicationPolicy
def create?
user.present? && (user.enable_post_meta_description == true)
end
def permitted_attributes
if user.enable_post_meta_description == true
[:title, :user_id, :meta_description]
else
[:title, :user_id]
end
end
end
# app/controllers/posts_controller.rb
class PostsController < ApplicationController
...
private
...
def post_params
params.require(:post).permit(
Feature::EnablePostMetaDescriptionPolicy.new(current_user, Post).permitted_attributes
)
end
end
<%= form_with(model: post) do |form| %>
...
<% if Feature::EnablePostMetaDescriptionPolicy.new(current_user, post).create? %>
<div class="field">
<%= form.label :meta_description %>
<%= form.text_area :meta_description %>
</div>
<% end %>
...
<% end %>