# : RailsStuff.load_modules = [] # : RailsStuff.load_modules = %i(sort_scope statusable)
@user
variable, but it's worse if @payment_transaction
. Worse, if @recurring_payment_transaction
. Someone then begins to abbreviate names, but this usually makes it even worse.before_action :set_user
/ before_action :find_manager
. With long lists of only
or except
, which must not be forgotten to keep up to date.@_resource
and @_collection
. No need for bulky constructs from set_collection_ivar
and get_resource_ivar
.belongs_to
and relationship chains. But there is a resource_helper
and a quick way to set source_relation
: resources_controller source_relation: -> { manager.projects } # #manager, params[:manager_id]: resource_helper :manager
def permited_params
): permit_attrs :name, project_attributes: [:id, :_destroy, :name]
respond_with
used only in create, update, destroy. In general, this should not be a problem: redefining .to_json
is not a good idea for formatting responses for the API. But if it is really necessary, you can add a module and add the necessary actions. module Site class UsersController < SiteController resources_controller permit_attrs :name, :email end class ProjectsController < SiteController resources_controller sti: true resource_helper :user permit_attrs :name permit_attrs_for Project::External, :company permit_attrs_for Project::Internal, :department def create super(action: :index) end protected def after_save_url url_for action: :index, user_id: resource.user_id end def source_relation params.key?(:user_id) ? user.projects : self.class.resource_class end end end
require_dependency/eager_load
for all inheriting models. Yes, and DescendantsTracker for getting a list of all inherited classes in this case is not the best solution. For such tasks we use TypesTracker. It contains a helper for loading all types for the model and stores a separate array with all the inherited classes: the list is ready and available at any time. In this case, you can selectively remove some classes from this list. class Project extend RailsStuff::TypesTracker # ... eager_load_types! # .rb app/models/project # : eager_load_types! 'lib/path/to/projects' end class Project::Big < Project unregister_type # end class Project::Internal < Project::Big; end class Project::External < Project::Big; end class Project::Small < Project; end Project.types_list # [Internal, External, Small]
class Project extend RailsStuff::TypesTracker # MyTaggedArray #add(klass, *args). # *args - *tags. self.types_list_class = MyTaggedArray end class Project::Internal < Project::Big # types_list.add Project::Internal, :tag_1 register_type :tag_1 end class Project::External < Project::Big register_type :tag_2 end Project.types_list.with(:tag_1)
RecordNotUnique
error RecordNotUnique
, repeat the previous step. # - SecureRandom.hex(32) random_uniq_attr :token # : random_uniq_attr(:code) { |instance| my_random(instance) }
helpers.actions
and / or helpers.confirmations
translation files: ru: helpers: actions: edit: delete: confirm: ? confirmations: delete: ?
# : # include RailsStuff::Helpers::Translation = translate_action(:edit) or translate_action(:delete) - collection.each do |resource| tr td= resource.name td= link_to 'x', url_for(resource), method: :delete, data: {confirm: translate_confirmation(:delete)} = translate_confirmation(:purge_all) # : '?'
# . # rails_stuff/helpers/links include RailsStuff::Helpers::Links ICONS = { destroy: '<span class="glyphicon glyphicon-trash"></span>'.html_safe, edit: '', new: -> { translate_action(:new) }, } def basic_link_icons ICONS end # : link_to_edit([:edit, :scope, resource]) or link_to_edit(edit_path) link_to_edit('url', class: 'text-info') # url , url_for(action: edit). # /users/1 link_to_edit # '/users/1/edit' # : link_to_destroy or link_to_new
response.json_body
for testing API;Net::HTTP
;gem 'rails_stuff', '~> 0.4.0'
adding gem 'rails_stuff', '~> 0.4.0'
in the Gemfile. More details, documentation in English and source codes are available in the githaba repository .Source: https://habr.com/ru/post/268359/
All Articles