X


[ Pobierz całość w formacie PDF ]
.Plugins aren t only useful for sharing new features: Plugins are used to test alterations to the Rails frameworkitself.Almost any significant new piece of functionality or patch can be implemented as a plugin and road-tested easily by a number of developers before it is considered for inclusion in the core framework.Whetheryou find a bug in Rails and figure out how to fix it or you come up with a significant feature enhancement,you will want to put your code in a plugin for easy distribution and testing.Of course, changing significant core behavior of the framework demands a solid understanding of how Railsworks internally and is beyond the scope of this book.However, some of the techniques demonstrated willhelp you understand the way that Rails itself is implemented.19.1.1 Plugins as RubyGemsAll popular Rails plugins are published as RubyGems.To install, you just have to add them to your Gemfileand runrakebundle.David recommends that authors with popular, version-released plugins, and especially477 Extending Rails with Plugins 478ones with dependencies, distribute their plugins as gems.Interestingly, as of Rails 3, all the major component frameworks of Rails are essentially plugins themselves.19.1.2 The Plugin ScriptTherails plugin installcommand shouldn t be necessary very often anymore since popular plugins arenow usually distributed as gems.Nonetheless, for legacy reasons we cover the plugin command here.1 $ rails plugin install plugin_urlNote that it should be run from the root directory of the application you are developing and the URL mustpoint to either a Git or Subversion repository.1 $ rails plugin install git://github.com/mislav/will_paginate.git2 Initialized empty Git repository in /Users/obie/hashrocket/three/vendor/plu\3 gins/will_paginate/.git/4 remote: Counting objects: 58, done.5 remote: Compressing objects: 100% (55/55), done.6 remote: Total 58 (delta 3), reused 26 (delta 0)7 Unpacking objects: 100% (58/58), done.8 From git://github.com/mislav/will_paginate9 * branch HEAD -> FETCH_HEADChecking thevendor/plugins, you can see that a directory namedwill_paginatehas appeared.The plugininstall command deletes the.gitdirectory, so that you can add your new plugin easily to your own sourcecontrol.You also get an easy way to remove plugins, by name.1 $ rails plugin remove plugin_nameQuite appropriately, this command performs the opposite of install: It removes the plugin fromvendor/plugins:1 $ rails plugin remove will_paginateA quick inspection of yourvendor/pluginsdirectory shows that thewill_paginate folder has indeed beenremoved completely.You can manually delete the plugin s directory, but running theremovecommand willalso run the plugin suninstall.rbscript, if it has one.19.2 Writing Your Own PluginsAt some point in your Rails career, you might find that you want to share common code among similarprojects that you re involved with.Or if you ve come up with something particularly innovative, you mightwonder if it would make sense to share it with the rest of the world.Rails makes it easy to become a plugin author.It even includes a plugin generator script that sets up the basicdirectory structure and files that you need to get started: Extending Rails with Plugins 4791 $ rails generate plugin my_plugin2 create vendor/plugins/my_plugin3 create vendor/plugins/my_plugin/init.rb4 create vendor/plugins/my_plugin/install.rb5 create vendor/plugins/my_plugin/MIT-LICENSE6 create vendor/plugins/my_plugin/Rakefile7 create vendor/plugins/my_plugin/README8 create vendor/plugins/my_plugin/uninstall.rb9 create vendor/plugins/my_plugin/lib10 create vendor/plugins/my_plugin/lib/my_plugin.rb11 invoke test_unit12 inside vendor/plugins/my_plugin13 create spec14 create spec/plugins/my_plugin_test.rb15 create spec/test_helper.rbThe generator gives you the entire set of possible plugin directories and starter files, even including a/tasksfolder for your plugin s custom rake tasks.Theinstall.rbanduninstall.rbare optional one-time setupand teardown scripts that can do anything you want them to do.You don t have to use everything that screated by the plugin generator.The two defining aspects of a plugin are the presence of theinit.rbfile and of a directory in the plugin calledlib.If neither of these exists, Rails will not recognize that subdirectory ofvendor/pluginsas a plugin.In fact,many popular plugins consist only of aninit.rbscript and some files inlib.19.2.1 The init.rb HookIf you pop open the boilerplateinit.rbfile that Rails generated for you, you ll read a simple instruction.1 # insert hook code hereHook code means code that hooks into the Rails initialization routines.To see a quick example of hook code inaction, just go ahead and generate a plugin in one of your projects and add the following line to itsinit.rb:1 puts "Current Rails version: #{Rails::VERSION::STRING}"Congratulations, you ve written your first simple plugin.Run the Rails console and see:1 $ rails console2 Current Rails version: 3.pre3 Loading development environment (Rails 3.pre)4 >>Code that s added toinit.rbis run at startup.(That s any sort of Rails commands, includingserver,console,andrunner.) Most plugins have theirrequirestatements ininit.rb. Extending Rails with Plugins 48019.2.1.1 Initialization VariablesA few special variables are available to your code ininit.rb:name The name of your plugin ('my_plugin'in our simple example) [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • funlifepok.htw.pl
  •  

    Drogi użytkowniku!

    W trosce o komfort korzystania z naszego serwisu chcemy dostarczać Ci coraz lepsze usługi. By móc to robić prosimy, abyś wyraził zgodę na dopasowanie treści marketingowych do Twoich zachowań w serwisie. Zgoda ta pozwoli nam częściowo finansować rozwój świadczonych usług.

    Pamiętaj, że dbamy o Twoją prywatność. Nie zwiększamy zakresu naszych uprawnień bez Twojej zgody. Zadbamy również o bezpieczeństwo Twoich danych. Wyrażoną zgodę możesz cofnąć w każdej chwili.

     Tak, zgadzam się na nadanie mi "cookie" i korzystanie z danych przez Administratora Serwisu i jego partnerów w celu dopasowania treści do moich potrzeb. Przeczytałem(am) Politykę prywatności. Rozumiem ją i akceptuję.

     Tak, zgadzam się na przetwarzanie moich danych osobowych przez Administratora Serwisu i jego partnerów w celu personalizowania wyświetlanych mi reklam i dostosowania do mnie prezentowanych treści marketingowych. Przeczytałem(am) Politykę prywatności. Rozumiem ją i akceptuję.

    Wyrażenie powyższych zgód jest dobrowolne i możesz je w dowolnym momencie wycofać poprzez opcję: "Twoje zgody", dostępnej w prawym, dolnym rogu strony lub poprzez usunięcie "cookies" w swojej przeglądarce dla powyżej strony, z tym, że wycofanie zgody nie będzie miało wpływu na zgodność z prawem przetwarzania na podstawie zgody, przed jej wycofaniem.