Merb Slices

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    2 Favorites

    Merb Slices - Presentation Transcript

    1. Merb Slices
    2. Slice By Who? • Fabien Franzen a.k.a. loob2 • Actually remembers Merb 0.0.0.1 a.k.a ‘the pastie’ • Representing Belgium (or maybe not...)
    3. Who Am I? • Daniel Neighman a.k.a. hassox • Engine Yarder • Proud Aussie
    4. What’s a Merb Slice?
    5. What’s a Merb Slice? • Full Stack Mini Merb Application • Designed to be shared between applications • Built on Merbs Public API
    6. What’s Included? • Full MVC Components • Assets • Routes • Full Namespacing • Hot Code Loading / Unloading • Distribution as Gems
    7. Slice It
    8. Slice It • Get it: ➡ $ sudo gem install blog_slice
    9. Slice It • Get it: ➡ $ sudo gem install blog_slice • Install it: ➡ $ rake slices:blog_slice:install
    10. Configure It
    11. Configure It config/init.rb dependency \"merb-slices\" dependency \"blog_slice\"
    12. Configure It config/init.rb dependency \"merb-slices\" dependency \"blog_slice\" config/routes.rb Merb::Router.prepare do all_slices end
    13. Run It
    14. Run It $ merb
    15. Use It
    16. Use It • By Default, your slice is available at: • http://localhost:4000/blog_slice
    17. Use It • By Default, your slice is available at: • http://localhost:4000/blog_slice • Get the posts: • http://localhost:4000/blog_slice/posts
    18. Rake It
    19. Rake It There are many rake tasks available
    20. Rake It There are many rake tasks available
    21. Rake It There are many rake tasks available • rake slices
    22. Rake It There are many rake tasks available • rake slices • rake slices:install_as_gem
    23. Rake It There are many rake tasks available • rake slices • rake slices:install_as_gem • rake -T slices:blog_slice
    24. Common Rake Tasks • rake slices:blog_slice:copy_assets • rake slices:blog_slice:patch • rake slices:blog_slice:freeze(:*) • rake slices:blog_slice:migrate • rake slices:blog_slice:spec
    25. Customize It • It’s just ruby. Monkey Patch it • Use the “patch” rake task • Slice code is in your app: • Merb.root/slices/blog_slice
    26. Custom Options
    27. Custom Options • Each Slice has a Configuration Hash • Merb::Slices::config[:blog_slice]
    28. Custom Options • Each Slice has a Configuration Hash • Merb::Slices::config[:blog_slice] • Aliased To: • BlogSlice[]
    29. Custom Actions
    30. Custom Actions Adding the Action to the controller $ rake slices:blog_slice:patch
    31. Custom Actions Adding the Action to the controller $ rake slices:blog_slice:patch Edit: Merb.root/blog_slices/app/controllers/posts.rb def publish @post = Post.first(:permalink => params[:permalink]) if @post.publish! redirect slice_url(:post, @post), :message => \"Posts Published\" else render :edit end end
    32. Custom Views
    33. Custom Views $ rake slices:blog_slice:freeze:views
    34. Custom Views $ rake slices:blog_slice:freeze:views Available in: Merb.root/slices/blog_slice/app/views
    35. Custom Views $ rake slices:blog_slice:freeze:views Available in: Merb.root/slices/blog_slice/app/views Add templates for additional formats
    36. Custom Layout
    37. Custom Layout • By Default the Slice layout is used
    38. Custom Layout • By Default the Slice layout is used Merb::BootLoader.after_app_loads do BlogSlice[:layout] = :blog_slice_layout end
    39. Custom Models
    40. Custom Models Stubs or Freeze? $ rake slices:blog_slice:freeze:models
    41. Custom Models module BlogSlice class Post property :published, Boolean property :published_at, DateTime Stubs or Freeze? def publish! self.published = true $ rake slices:blog_slice:freeze:models self.published_at = DateTime.now save end end # Post end # BlogSlice
    42. Custom Routes Merb::Router.prepare do add_slice(:blog_slice, :path_prefix => \"blog\", :default_routes => nil) do identify BlogSlice::Post => :permalink do match(\"/posts/:permalink/publish\"). to(:controller => \"posts\", :action => \"publish\"). name(:publish) end end end
    43. Prefix URL Paths
    44. Prefix URL Paths • Setup a Path Prefix • add_slice(:blog_slice, \"awesome\") • Example: /awesome/posts
    45. Prefix Named Routes
    46. Prefix Named Routes • Default name_prefix on named routes: • add_slice(:blog_slice) • url(:blog_slice_posts)
    47. Prefix Named Routes • Default name_prefix on named routes: • add_slice(:blog_slice) • url(:blog_slice_posts) • Setup a Name Prefix • add_slice(:blog_slice, :name_prefix => \"blog\") • url(:blog_posts)
    48. Write It
    49. Development Flow
    50. Development Flow •Generate •Write •Use •Write •Use •Finish - Install
    51. Development Flow •Generate •Write •Use •Write •Use $ slice •Finish - Install
    52. Generate It $ merb-gen slice blog_slice
    53. New Slice Structure blog_slice blog_slice |-app |-public |---controllers |---images |---helpers |---javascripts |---models |---stylesheets |---views |-spec |-----layout |---controllers |-----main |-stubs |-lib |---app |---blog_slice |-----controllers |-pkg |-----models
    54. Some Important Files blog_slice/lib blog_slice merbtasks.rb slicetasks.rb < Here be dragons spectasks.rb blog_slice.rb
    55. Initialize It
    56. Initialize It • blog_slice.rb is where the slice initializes
    57. Initialize It • blog_slice.rb is where the slice initializes • Dependencies
    58. Initialize It • blog_slice.rb is where the slice initializes • Dependencies • Router
    59. Initialize It • blog_slice.rb is where the slice initializes • Dependencies • Router • Hooks
    60. Fake It • Setup a fake “host app” env in config/init.rb • config/init.rb is not used normally
    61. Controllers
    62. Controllers • blog_slice/app/controllers/posts.rb class BlogSlice::Posts < BlogSlice::Application # Your Controller Code Here end # Posts
    63. Views • blog_slice/app/views/posts/show.html.haml • blog_slice/app/views/layouts/application.html.haml
    64. Layouts • blog_slice/lib/blog_slice.rb Merb::Slices::config[:blog_slice][:layout] ||= :blog_slice
    65. slice_url
    66. slice_url • Use slice_url for url generation inside a slice • slice_url(:controller => ..., :action => ...) • slice_url(:post, @post) • slice_url(:merb_auth_slice_password, :login)
    67. Models Namespace your models (you don’t have to) class BlogSlice::Post include DataMapper::Resource property :id, Serial property :title, String, :lenth => 255 property :body, Text property :slug, Slug before :save do self.slug = self.title unless self.permalink end end # BlogSlice::Post
    68. Assets
    69. Assets • blog_slice/public • images, css, javascipt • Read: blog_slice/app/helpers/application_helper.rb
    70. Images • blog_slice/public/images • Helper: image_path(image)
    71. Javascript • blog_slice/public/javascripts • Helper: javascript_path(javascript)
    72. Stylesheets • blog_slice/public/stylesheets • Helper: stylesheet_path(style)
    73. Install Assets • rake slices:blog_slice:copy_assets
    74. Route It blog_slice/lib/blog_slice.rb (the init.rb) def self.setup_router(scope) scope.identify Post => :slug do resource :posts end end
    75. Hook It
    76. Hook It • loaded
    77. Hook It • loaded • init
    78. Hook It • loaded • init • activate
    79. Hook It • loaded • init • activate • deactivate
    80. Hooks - loaded • Slice Code Loads • Hook - loaded • Boot Loader LoadClasses
    81. Hooks - init, activate • Slice Hook - init • BootLoader AfterAppLoads • Slice Hook - activate: triggered by Merb::Slices.activate(BlogSlice)
    82. Hooks - deactivate • Triggered by Merb::Slices.deactivate(BlogSlice)
    83. Spec It • Specs go in blog_slice/spec • rake -T spec • Spec your slice like an app
    84. Spec It - Setup • Setup the routes: before :all do Merb::Router.prepare do add_slice(:blog_slice) end if standalone? end
    85. Distribute It • rake gemspec • rake install
    86. Questions?

    + hassoxhassox, 9 months ago

    custom

    2412 views, 2 favs, 0 embeds more stats

    Presentation given at merb-camp 08 outlining merb-s more

    More Info

    © All Rights Reserved

    Go to text version
    • Total Views 2412
      • 2412 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 2
    • Downloads 67
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as innappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel

    Categories