ruby on rails - How to write a gem? Preparing with 'lib' directory -


can explain few moments? want create gem, me, self-study.

for this, write gem`s code in 'lib' folder, can test in app.

for now, have next questions:

  1. how can extend 'views'? want create separate layout.
  2. how can extend controllers? example - applications controller, want extend few methods.

for extended user model, this:

#app/models/user.rb class user < activerecord::base   require 'auth' ...    #lib/auth.rb class user   require 'digest'    before_save :encrypt_password    def xxx .... 

is right way?

for now, need extend application controller , add application layout, not know how.

you can check out gem made if like

--

gems

rails gems zip-files rails app

when "install" rails gem, gem files inside gem put rails application. take our gem example:

enter image description here

the folders marked in red inserted rails application when install gem. can't see them, they'll there


engine

it's opinion majority of rails gems engines:

this means whenever create gem, you're doing creating specific piece of functionality can applied application required

a gem create module, wrap gem's functionality. module defined in lib file created, , therefore allow extend gem's functionality inheriting module


views / controllers

to create views / controllers in application, need remember said - files gem placed app

with in mind, way create /app folder, , put views or controllers directories in there:

enter image description here

you need create directories inherit module define in lib directory of gem:

-app |-controllers |--exception_handler |---exception_controller.rb  |- views |-- exception_handler |--- exception |---- show.html.erb  -lib |-exception_handler.rb #-> exceptionhandler module 

this same if namespace routes

--

as i've created gem, may best talking me directly process. can post comment if you'd talk further


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -