Do your views know too much?

Video in TIB AV-Portal: Do your views know too much?

Formal Metadata

Do your views know too much?
Title of Series
Part Number
Number of Parts
CC Attribution - ShareAlike 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this license.
Release Date

Content Metadata

Subject Area
The logical place to put view-related logic is... inside your view, right? "A little logic here... a little logic there..." but all of a sudden we hardly recognize our views. A quick glance through our code and we can't tell our Ruby apart from our HTML. Don't worry; this is a fun opportunity for some refactoring! Come see several approaches you can start using today to clean up your views.
Mathematics View (database) Summierbarkeit
Data mining Multiplication sign Weight Connectivity (graph theory) Source code Materialization (paranormal) Entropie <Informationstheorie> Speech synthesis Video game Social class 2 (number)
Computer animation View (database) Cellular automaton Multiplication sign Speech synthesis
Web page Complex (psychology) Texture mapping Code Multiplication sign System administrator Electronic mailing list Data storage device Database Goodness of fit Term (mathematics) Profil (magazine) Semiconductor memory Order (biology) Right angle Condition number
Workload Data mining Game controller Service (economics) Meeting/Interview Code Multiplication sign Factory (trading post) Software design pattern Right angle Object (grammar) Endliche Modelltheorie
Code Multiplication sign View (database) Debugger Electronic program guide Online help Event horizon Expected value Type theory Computer animation Meeting/Interview Video game Software framework Pattern language
Point (geometry) Game controller Implementation Code Real number Execution unit Sheaf (mathematics) Drop (liquid) Mereology Power (physics) Element (mathematics) Neuroinformatik Number Programmer (hardware) Different (Kate Ryan album) Energy level Endliche Modelltheorie Form (programming) Dependent and independent variables Email Electric generator Forcing (mathematics) Sound effect Database Denial-of-service attack Instance (computer science) Variable (mathematics) System call Vector potential Computer animation Right angle Object (grammar) Row (database)
Domain name Presentation of a group Game controller Functional (mathematics) Code View (database) Fitness function Mass Computer animation Term (mathematics) Logic String (computer science) Self-organization Software testing Right angle Endliche Modelltheorie Object (grammar) Writing
Computer animation Electronic mailing list Bit Form (programming)
Computer animation Computer file Personal digital assistant String (computer science) Electronic mailing list Software testing
Default (computer science) Arithmetic mean Computer animation Multiplication sign Bit Data structure
Word State of matter Wrapper (data mining) Cuboid Right angle Object (grammar) Endliche Modelltheorie Number
Filter <Stochastik> Presentation of a group Group action Code Structural load Projective plane Set (mathematics) Device driver Insertion loss Cartesian coordinate system Metadata Computer animation Software repository Personal digital assistant Logic Video game Object (grammar) Social class
Game controller Computer animation Link (knot theory) Factory (trading post) Device driver Code refactoring Object (grammar) Condition number
Logical constant Complex (psychology) Game controller Group action Link (knot theory) Code Multiplication sign View (database) System administrator Element (mathematics) Partial derivative Condition number Block (periodic table) Electronic mailing list Bit System call Symbol table Word Computer animation Personal digital assistant Logic Factory (trading post) Order (biology) Quicksort Abstraction Row (database)
Slide rule Group action Context awareness Presentation of a group Code Multiplication sign Decision theory Device driver Rule of inference Revision control Medical imaging Mathematics Lattice (group) Different (Kate Ryan album) Authorization Arrow of time Software testing Endliche Modelltheorie Data structure Partial derivative Form (programming) Condition number Addition Cellular automaton Projective plane Mathematical analysis Content (media) Instance (computer science) Variable (mathematics) Word Bootstrap aggregating Logic Video game Right angle Pattern language Object (grammar) Quicksort Local ring
this work the and this and so I appreciate you coming out to listen to me ramble about the used so there is much reward and that once we are done here we get near Madison keynote so much to look forward to we gotta get through this sum but today talking about the use it and when I originally put this talk together I really want to pose the question for myself and that was due my views know too much and so this altogether I thought well actually have a change of mind is thoroughly question I can pretty much probably say your views known to watch all so related today before we get to that marriage
Jason and write really since 2012 and this materials I work for a company backroom called winds rentals and 90 per cent of the time a literals people always respond with what how we write him equipment nationwide where rail shop also new dad my wife was really excited when that's always going to be gone for a week and she has a tunable at home so yeah so the other thing come under sources for it started is that it's amazing to build the sentence be today i because I'll the general use be terrified of
this about 3 years ago I I have a really crippling anxiety disorder and 24 people used terrify me so much so that in college actually to my public speaking class all mine and I have to think it's amazing of but 3 years ago at rails can't they had some technical difficulties lighting talks instantaneous weighted components on them as I will talk about my anxiety I did it for 90 seconds and it literally state change my life to where like I I crave coming to talk so that was a seduced useful nothing but
I I would regret not that because if you want every public speaking you should because it's amazing and there's not a better community that this 1 cell anyway your views know too much of died so
know too much what is that even mean so as assisted thinking about it and really have seen this many times because I'm the 1 that writes it many times argues just can
become this dumping ground without realizing it all of our sense of good coaches gets thrown away when we start right use I I just say other just use of why many maybe that's not the best thing right so take for example this following chunk of code losing B. if you're handling is round sorry this is easier for me today on you've probably seen code like this and if you have it will you could really consider yourself lucky my opinion but not which is so easy to get here so let's walk through can with this is because this will be looking at all times 1st order and is where all users of a database and then we're looking throw simple enough right the syringes relevant list item and maybe some until click the user's name and the texture profile page OK simple enough requirements change as they always do terms well if the current user sees himself on the list such build click edit OK so linked to an edit user path simple enough well also uses an admin this built to delete users proconsul check and see memories in devising the apparent user method we should see the advent and the more willing to the so assigned to some place but there's a lot there in the question is why how did we get
there the well the complexity peligro over time right IP-related solve the bunch conditionals interviews but so that has struck me 1 after the other the store on you see
I will talk about clean code mine like my favorite thing is like will talk design patterns would stop at factory workload belongs I will preach skinny controller I'll preached in model I will tell you service objects in Aspen time learning about this stuff because I really enjoy it I don't know if it's something about it makes me feel smart but I enjoy it so there's a reason we do this and that's because those things are good right overall the make our code
better than they make us better but notice a trade my
life and that is well wasn't done
refactoring in implementing all the patterns I just stopped and say up there's use of it and everything I can hold valuable I just let it go so I wanna spend my time today with you talking about refocusing on views and you're interviews and love before do that was so close to think this talk is soaring events of expectations this is the talk about any kind of design because I'm not a designer I can help you make your code the better the outside this is the talk of another type of you might heard of of when it comes to serve from front end JavaScript's frameworks and nothing at all to on that but this is also not a definitive guide these are just things about learned that help me and so I hope that I hope you but it's OK if we disagree so with that said
will start doing in effects in art so then users the people before has ever thought about this problem so let's go through this chapter code and let's look at it section by section and look for what will call code smells but really they're not definitive cancels the self proclaimed code smells which obviously my better right so this so it was with her using initiate an z 1 sees the potential will call coastal here OK so are you should know about a database right now Active Record makes it really easy for us to access models anywhere anytime it's fantastic but the old saying with great power comes great responsibility are you should know the difference between an active record collection Anderson array it should have to care about that In fact I I think about 2 1 day if you need to change so you're getting users is easier to go to a controller or take review found so say we should give argues data to work
with they should care about implementation a lovely Israel's builders we know very easy fixes it we can use instance variables so I will say that this is an ad controller right maybe it has posted it is to show that all the so call user on all and already we get a quick wins Arcos easier to read and you can look at the person said OK I'm working with users you don't have to grab the user about all that each so with that gun was keep chipping away the western even with this 1 this was a little less harmless right the so inherently there's really nothing wrong with this but I wanna focus in on 1 thing and that's the generation of the name right now this is fine what happens when we need to use that form and somewhere else they were using the navigation when users flood and they will send an e-mail the well you'd be right that over and over again the school per bows and as real programmers were taught to not repeat ourselves listen very dry for haven't always concatenated 1st last name it is highly likely that we use that a lot so this next part really deals with 2 things the 1st 4 is are you should repeat themselves now there will be additional elements the Derby themselves with as far as computing data well it should have to do that which also brings us to the next point are you should really have to compute data show the responsible for showing the data but we should have to ask them to actually do the work Cyrillic at 3 ways to solve this and so because of the court will talk about today so we'll call these are the 3 ways to drop code Dry number 1 of we could fix a city unit level so were done with the user object here just sensitive user and we have a force Célestin on that object so should
not on user we can just have a name as an returns a string with the 1st or last name and sure enough whenever we want access this which scholars about 9 this works fine any gives us our 1st benefit is of course write tests for it Brezalauspurc that we can just get user search a user we want a test of the name because of resource entry of this work better yet in our code as well notice a user and soul easier to read was brainpower universal phonetic so it's clear in my opinion it's reusable it's testable earlier on my career and say this is great what's packing up work here is done but now remember I love talking about organization of clinker so the more I kind of dug into this I learned about this term this phrases called presentation
logic is about here from the presentation logic empirical so essentially we have objects the that joke that on the data for our to show to the user so this kind logic doesn't use in the business domain at all it's just for presenting data OK so that makes sense right but where does that belong because the the a models are a good fit for that right OK so within about a rails we we can do that well so when we can drop occurs Wikinews holders who here is used Alva yes it should be all us because rails is full of helpers you make a written custom holders OK so if you're unfamiliar helpers are functions that we have access to and every view every controller so so the built-in helpers are dealing with assets of massive pipeline making dates prettier making forcefully user work with things like that and actually this is
a this work does a complete list
of all the helpers available in rails the so is quite a bit they're quite useful thanks for
catching things 1st dates we
have all these available to seize role helpers forms because on and on if you will see that
lists I with the and wrote them all out for my own sanity and they're available there OK so there are cases that we didn't see the through a helper the hopeless a full name so will create our own the rails inside acts helpers we have our we generate uses helper file and we just have a method called full it takes any user and returns a string OK so the nice thing is we can just put that to argue right here you'll notice that actually now we using 3 rails holders we have a way to you the there we have our custom solver full name and user path which is dynamically Jerry method to episcopus it uses this still testable will switch over to you many test because rails makes a pretty easy and it will just assert that hate whenever I give this method uses as a 1st or last name it should make sense the OK great what's again this works back it up we're done the but remember were not done
so discovery thinking the doing
helpers for every bit data the 1 work with you know helper a really useful they provide a great value to us but that doesn't mean that and what every helper and every request because a lot of times I not be working with the user In by default rails automatically includes a lot helpers on every request In fact the
178 methods alone from those which may be her DHH Skinner this is in 1 of the roles structure this is fine but for me at all adding more that I don't have to I know this what is be might it's not wrong these helpers but now we think is a better way and this is when I found
decorators so the best way to explain decorator is open up microsoft paint
so let's say that we have a model may be any any really object will states and sense of our user and that's this red box right here we instantiated in what we wanna do is pass it to another object and this is our decorator this blue box so it goes inside a data what we do is we have a decorator is just a wrapper for object who were working with so they're saying we can do from there is that any method available to are the aim of the real more original objects we some existing but since we now have a rat object we can call other methods it's a lot words don't worry I'm going to show you you can actually do this number ways you can write your own Ruby objects to this but since we're efficient
filters would issues agenda not exist the gym is called Draper
so that give repo as a draper as an object oriented layer of presentation logic there's that phrase again 2 Rails application contact what is this mean what is a signal of life so this is a decorator just a class the merits of our case from driver decorator you notices method here called delegate all so what happens is the best way out on this is with the example of how there's always the the example of as code so maybe are we have a use Roger transit of in what we wanna do is there is no presentation logic for that we can actually rely on our object to give us that what delicate all does say hey if this method is not defined here were not the findings of the let's go to the object wrapping to get that the the other thing we have here is a common code on how to generate had he's helpers Albert when Ireland metadata helpers in this case a nice because there it doesn't load the whole set of all percentages as per needed there's of the a there's no magic here really of decorative pretty straightforward easy-to-use so let's go ahead and add that name we keep talking about OK so 1 of the common cold and would say the findings and will say here object or person not a loss object is how we reference are that's what our object for having some sure enough this works it's still testable and users back our wanna go but the thing about objects by their projects you have to actually instantiate them so there's several ways to do this which would have 3
ways the 1st way is you can actually was a driver Jim just call decorate on active references you can also actually call stack mythical decorated on user decorator and pass an object or if you're dealing with the collection you can say he's a decorator therefore a collection so the 1 we want because you're dealing with users is a staggering collection so we'll just adds to or controller and will we have user not all and will just say what we wanna decorate that's was the user decorator decorate collection and sure enough we go back this works what so there's a lot 0 so with me yes thank you there's a lot of 1 of terms so that we can keep on our the factory so it doesn't look at 3 ways we do you know little refactoring we the refactoring we continue decorator refactoring the thing about this those those old deal with objects right we have something we have an object we actually work with but what about here while we're saying we have a user and if the current user equals the user then show this who OK well 1 of the really fits right and it's really fit with the object is couple different things going on here so 1st of the smell you should know about conditionals alive this is so hard to avoid and it's so easy to do real rails makes it so easy which uses waiting get out of and so were
not deal with an object prosaic resource Jerry link the so maybe helpers against here OK so 1 we know
that if the user were displaying many of user were spies occurring so we can maybe may help a method called link it was or conditional on here will say we want at late for the user will pass will say the user equals user and what you what they wanna bring up the notice that not passing the current user only the user that's because remember these are are included every controller to feasible like devised for users controller action so you already have access to the OK so this works the let's say we we know we need a list item so this is what that looks like a little stardom on with a link to edit a so if you we read a short distance of conditional but do we really wanna write raw HTML in a room you seems have the opposite of forward trying to do so all rely on another helper called constant time or counterintelligence it'll take whatever HTML element you what is the symbol in our case list item In order to pass block with what should be inside list well that's easy enough because we know we need a way which is also help so sure enough we can
just pass Arlington their link to edit but MRF is complete we did removal logic per say which is extracted out of our view so you cares about is this no this code in my is easier to read it also it might be harder to body because your conditionals abstracted but I think this is a better when so we do the same thing for admin link will take this fall traditional shown to help the so say the current uses an admin was overlooked without and a was sort of the of Italy this so now argues have cleaned up quite a bit in fact the last word factory and this is what we're left with well we took out the Active Record call resolving to users who are linked to a user's name urges profile show the and the now we can look an unordered list and say will issue an edit link here this is also the the solver methods we deferral successful as well it's not that his chances are that today you might ask what about partials it's a great question I was so you should use partials you should use parcels as much as you want and but I would argue that maybe we should use partials to hide complexity by such as conditionals things like that still use your partials but also to try and find other ways get conditionals that use so just a recap there's 4 things that we did today are due now doesn't know about how simple how things are implemented it doesn't have to repeat itself it doesn't have to compute data it doesn't have to account for conditionals important to me about all this is obvious no less another pretty much responsible for 1 thing and that's
displaying data to Iran through that's that's
all I got but yeah the you're coming out and I have plenty of time to the questions authority adopt so the question was no to have a preference on passing and local variables instance variables 2 partials I don't have a particular profits from it work we've pretty much as had decided that we're gonna use locals for that and and that's word alright for us all if you're coming to a code base you have seen before it's like it's easier to like look at that rendered light render partial and see was being passed on but I don't particularly have a strict preference on you could call the question was use use you models so that writers you can all the we use a decorator pattern or the deck the driver Jim mostly because of 1 was was because it's what I've inherited at but people also because I like to the sort of thing and or the cell yeah I like the old you reliance on things again like being able access all persons like that the missile easier for me but when he said you while you talk about just making light actually models that adjusted fossil forms when data right some than others some like an army does with like the models of the yeah you could do that on I don't I will have a preference either way I decided what your preferences for decorator but I haven't land you models like just like my objects right so the question was there so with no values and their objects the US alone my entire life is dealing with no values of yeah but it will I guess the revenge I have as you know the register to use all of a place to deal with that it's not interview it's it's so the cancer testing for is no values like that so yes so the question was asked if you are mixing manually running LI tags additional or ERB versus using helpers to generate a lattice ever right issues of yes the I've tried to you even if there's no conditional logic all still try and its it has like compute some data also trumpeted the helper only because for an example is our this is formal example this is why did recently along with bootstrap you know you have the carousel so for us we needed to you on you know dynamically update images sometimes social buttons by to click on some and then we also show the arrows so on economic structure that out too little helpers of so that we can reuse it throughout the site and that if we do you have to make a change in the there's no conditionals want to make it 1 place which you could argue that some of that Convention partials but since I started like sorrows and all president exploded across so yeah there's ways to mitigate because still the besides repeat yourself some so and new some analysis of the different 3 presenters and decorators I don't know I haven't worked with the presenter pattern on so far been limited things action today yeah for sure I'm not called the decision a difference of cells yet have a user seldom sorry I yes so the cells germ to the Trailblazer Project right and I really want to I really am intrigued by the whole altruism project but I haven't I haven't taken the time to Mottola annoyance and are not How do I like in the content any kind of on our life so I started off as a front and older which was like not my life calling but amend awareness residing in silicon 1st and I was like yeah I like as we use all the review things on I don't I'm just going to generate slide if I have nothing back from Ruby like this rules not and that it not worth it means but otherwise with you for coming out I really appreciate and come talk to me I will try to not miss flowers less what audio version of the year so I have to show
up in