by Rob Hope via One Page Love
"Mr Branding" is a blog based on RSS for everything related to website branding and website design, it collects its posts from many sites in order to facilitate the updating to the latest technology.
To suggest any source, please contact me: Taha.baba@consultant.com
Wednesday, March 1, 2017
Meetup
by Rob Hope via One Page Love
How to Choose the Best Fonts for Your Next Project with Fontcloud
You find yourself staring at the Photoshop loading screen. Again.
Another day where you feel it’s taking a few extra seconds. You start thinking what could be the cause. Maybe it was the latest font bundle with 30 new fonts I installed? Again.
Fonts have never been more accessible. On a daily basis hundreds are released all over the web. And it’s not only foundries anymore. Indie designers are on the rise and are taking the market by storm.
For designers, this is a great time to be alive. Never before where there so many fonts to choose from and never have they been more affordable.
But how do you manage all these fonts? How do you know which ones you snagged up for free and which ones you purchased?
Finding the right font for your project can be difficult. Not only remembering how they look, but also keeping track of the ones you are legally allowed to use.
Luckily there is a new free online tool to help you manage and select the right fonts.
What is Fontcloud?
Fontcloud is an easy to use font manager created to help you keep an overview. It allows you to access all your installed fonts via the browser for easy pickings.
Getting started
Fontcloud runs completely in the browser. It doesn’t require you to install any software and you don’t need an account for viewing your fonts.
Your browser does need to support flash. Unfortunately flash is still the only way to view what fonts you have installed.
Searching for the right font
To get started, you simply type the text you would like to preview and hit enter. Your installed fonts will automatically show up with your text, allowing you to browse through them.
There are a couple of controls that can help you narrow down your font list.
- Background color: See if the text fits multiple backgrounds
- Manage capitalization: Check how to font works capitilaized or lowercase.
- Font size: Test how eligible your fonts are.
- Filter selection: Create a selection of your top picks.
Sharing a selection
If you need some quick feedback on your selection – for example from your client – you can simply click “Share selection” and you will get a private share URL.
Your selection will be converted to an image, which means that the recipient won’t need to have your fonts installed in order to view your preview. This makes for easy collaboration.
Managing licenses
Another useful feature is the option to assign licenses to your installed fonts. As the licenses are stored on the server side, you do need a (free) account for this.
Simply click the gavel icon underneath a font and select the license.
By default you have 2 licenses available (commercial & personal use). You can manage your licenses by adding new ones or removing existing ones.
You can easily load a list to view all fonts that fall under a specific license. This way you can make sure you are legally in the clear.
Viewing PUA encoded characters
When you don’t have a glyphs panel available like the default one in Illustrator it can be a real pain to access the Private Use Area glyphs. Working with Windows Character Map is horrific.
Continue reading %How to Choose the Best Fonts for Your Next Project with Fontcloud%
by Roemie Hillenaar via SitePoint
Swift: Probably The Best Full-Stack Language in the World
Ever since its release in 2014, Swift went through multiple iterations in order to become a great full-stack development language. Indeed: iOS, macOS, tvOS, watchOS apps, and their backend can now be written in the same language.
Backends can be written in many other languages – but let us argue why Swift is probably the best full-stack language in the world for mobile developers.
Safety. An essential advantage of Swift as a perfect back-end programming language is the safety built into the language. Swift does away with entire classes of errors and crashes. Remember null pointer exceptions? Those that cause crashes when objects you expect not to be nil are accidentally nil. Swift’s optionals let you know in advance if an object may be nil, and if so, force you to adequately handle the nil case. Safe initialization prevents you from ever initialising an object such that it ends up being nil. Remember unrecognized selector sent to instance crashes? Swift is type-safe meaning that if you’re calling a function on an object that doesn’t respond to it, the error will be caught by the compiler and not at runtime. Yet Swift was explicitly designed to be familiar and practical, rather than to adhere to some particular programming dogma. That said, as Chris Lattner puts it, “the defaults encourage safety and predictability”.
Continue reading %Swift: Probably The Best Full-Stack Language in the World%
by Ariel Elkin via SitePoint
Alessandro Scarpellini
by Rob Hope via One Page Love
OneBigTweet
by Rob Hope via One Page Love
This Week in Mobile Web Development (#147)
|
by via Mobile Web Weekly
Authorization With Pundit
Pundit is a tool that allows you to restrict certain parts of your Rails application to authorized users. It does this by providing you with certain helpers.
In this tutorial, you will build a blog that restricts parts such as creating, updating and deleting articles to authorized users only.
Getting Started
Start by generating a new Rails application.
rails new pundit-blog -T
The -T
flag tells Rails to generate the new application without the default test suite. Running the command will generate your Rails application and install the default gems.
Go ahead and add the following gems to your Gemfile. You will be using bootstrap-sass for the layout of your application, and Devise will handle user authentication.
#Gemfile ... gem 'bootstrap-sass' gem 'devise'
Run the command to install the gem.
bundle install
Now rename app/assets/stylesheets/application.css
to app/assets/stylesheets/application.scss
. Add the following lines of code to import bootstrap.
#app/assets/stylesheets/application.scss ... @import 'bootstrap-sprockets'; @import 'bootstrap';
Create a partial named _navigation.html.erb
to hold your navigation code; the partial should be located in app/views/layouts directory. Make the partial look like what I have below.
#app/views/layouts/_navigation.html.erb <nav class="navbar navbar-inverse"> <div class="container"> <div class="navbar-header"> <%= link_to 'Pundit Blog', root_path, class: 'navbar-brand' %> </div> <div id="navbar"> <ul class="nav navbar-nav pull-right"> <li><% link_to 'Home', root_path %></li> <ul class="nav navbar-nav pull-right"> <% if user_signed_in? %> <li><%= current_user.email %></li> <li><%= link_to 'Log out', destroy_user_session_path, method: :delete %></li> <% else %> <li><%= link_to 'Log In', new_user_session_path %></li> <li><%= link_to 'Sign Up', new_user_registration_path %></li> <% end %> </ul> </ul> </div> </nav>
For the navigation to be used, you need to render it in your application layout. Tweak your application layout to look like what I have below.
#app/views/layouts/application.html.erb <!DOCTYPE html> <html> <head> <title>Pundit-Blog</title> <%= csrf_meta_tags %> <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> </head> <body> <%= render "layouts/navigation" %> <div id="flash"> <% flash.each do |key, value| %> <div class="flash <%= key %>"><%= value %></div> <% end %> </div> <div class="container-fluid"> <%= yield %> </div> </body> </html>
Generate the User Model
Run the command to install Devise.
rails generate devise:install
Now generate your User model.
rails generate devise User
Migrate your database.
rake db:migrate
Generate Article Resources
Run the command to generate your Article resources.
rails generate scaffold Articles title:string body:text
This will generate your ArticlesController
and Article Model. It will also generate the views needed.
Now migrate your database by running:
rake db:migrate
Open up app/views/articles/_form.html.erb
and make it look like what I have below.
#app/views/articles/_form.html.erb <%= form_for(article) do |f| %> <% if article.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(article.errors.count, "error") %> prohibited this article from being saved:</h2> <ul> <% article.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :title %> <%= f.text_field :title %> </div> <div class="field"> <%= f.label :body %> <%= f.text_area :body %> </div> <div class="actions"> <%= f.submit %> </div> <% end %>
For your index file, it should look like this.
#app/views/articles/index.html.erb <table class="table table-bordered table-striped table-condensed table-hover"> <thead> <tr> <th>Title</th> <th>Body</th> <th colspan="3"></th> </tr> </thead> <tbody> <% @articles.each do |article| %> <tr> <td><%= article.title %></td> <td><%= article.body %></td> <td><%= link_to 'Show', article %></td> <td><%= link_to 'Edit', edit_article_path(article) %></td> <td><%= link_to 'Destroy', article, method: :delete, data: { confirm: 'Are you sure?' } %></td> </tr> <% end %> </tbody> </table> <br> <%= link_to 'New article', new_article_path %>
The above code arranges the articles on the index page into a table format to make it look presentable.
Open up your routes file and add the route for articles resources.
#config/routes.rb ... resources :articles root to: "articles#index"
Integrate Pundit
Add the Pundit gem to your Gemfile.
#Gemfile ... gem 'pundit'
Run the command to install.
bundle install
Integrate Pundit to your application by adding the following line to your ApplicationController.
#app/controllers/application_controller.rb ... include Pundit ...
Run Pundit's generator.
rails g pundit:install
This will generate an app/policies folder which contains a base class with policies. Each policy is a basic Ruby class.
This is how the base class policy looks.
#app/policies/application_policy.rb class ApplicationPolicy attr_reader :user, :record def initialize(user, record) @user = user @record = record end def index? false end def show? scope.where(:id => record.id).exists? end def create? false end def new? create? end def update? false end def edit? update? end def destroy? false end def scope Pundit.policy_scope!(user, record.class) end class Scope attr_reader :user, :scope def initialize(user, scope) @user = user @scope = scope end def resolve scope end end end
Create the Article Policy
Now you need to write your own policy. For this tutorial, you want to allow only registered users to create new articles. In addition to that, only creators of an article should be able to edit and delete the article.
To achieve this, your article policy will look like this.
#app/policies/article_policy.rb class ArticlePolicy < ApplicationPolicy def index? true end def create? user.present? end def update? return true if user.present? && user == article.user end def destroy? return true if user.present? && user == article.user end private def article record end end
In the above, you are permitting everyone (registered and non-registered users) to see the index page. To create a new article, a user has to be registered. You use user.present?
to find out if the user trying to perform the action is registered.
For updating and deleting, you want to make sure that only the user who created the article is able to perform these actions.
At this point, you need to establish a relationship between your Article and User model.
You do so by generating a new migration.
rails generate migration add_user_id_to_articles user:references
Next, migrate your database by running the command:
rake db:migrate
Open the User model and add the line that seals the relationship.
#app/models/user.rb ... has_many :articles
Your Article model should have this.
#app/models/article.rb ... belongs_to :user
Now you need to update your ArticlesController
so it is in sync with what you have done so far.
#app/controllers/articles_controller.rb class ArticlesController < ApplicationController before_action :set_article, only: [:show, :edit, :update, :destroy] # GET /articles # GET /articles.json def index @articles = Article.all authorize @articles end # GET /articles/1 # GET /articles/1.json def show end # GET /articles/new def new @article = Article.new authorize @article end # GET /articles/1/edit def edit end # POST /articles # POST /articles.json def create @article = Article.new(article_params) @article.user = current_user authorize @article respond_to do |format| if @article.save format.html { redirect_to @article, notice: 'Article was successfully created.' } format.json { render :show, status: :created, location: @article } else format.html { render :new } format.json { render json: @article.errors, status: :unprocessable_entity } end end end # PATCH/PUT /articles/1 # PATCH/PUT /articles/1.json def update respond_to do |format| if @article.update(article_params) format.html { redirect_to @article, notice: 'Article was successfully updated.' } format.json { render :show, status: :ok, location: @article } else format.html { render :edit } format.json { render json: @article.errors, status: :unprocessable_entity } end end end # DELETE /articles/1 # DELETE /articles/1.json def destroy @article.destroy respond_to do |format| format.html { redirect_to articles_url, notice: 'Article was successfully destroyed.' } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_article @article = Article.find(params[:id]) authorize @article end # Never trust parameters from the scary internet, only allow the white list through. def article_params params.require(:article).permit(:title, :body, :user_id) end end
At this point in your application, you have successfully implemented the policies that restrict certain parts of your application to selected users.
You want to add a standard error message that shows whenever a non-authorized user tries to access a restricted page. To do so, add the following to your ApplicationController
.
#app/controllers/application_controller.rb ... rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized private def user_not_authorized flash[:warning] = "You are not authorized to perform this action." redirect_to(request.referrer || root_path) end
This code simply renders a basic text that tells the user s/he is not authorized to perform the action.
Run:
$ rails server
To start your Rails server, point your browser to http://localhost:3000
to see what you have.
Conclusion
In this tutorial, you learned how to work with both Devise and Pundit. You were able to create policies that allowed only authorized users to view certain parts of the application. You also created a basic error text that shows when a non-authorized user tries to access a restricted part of the application.
You can learn more about Pundit by checking the GitHub page.
by Kingsley Silas Chijioke via Envato Tuts+ Code